5

这段代码应该只取消关注不关注的用户,但它会取消关注一些关注者,无法弄清楚原因。

$oTwitter = new TwitterOAuth (...)

$aFollowing = $oTwitter->get('friends/ids');
$aFollowing = $aFollowing->ids;
$aFollowers = $oTwitter->get('followers/ids');
$aFollowers = $aFollowers->ids;

$i=1;
foreach( $aFollowing as $iFollowing )
{
$isFollowing = in_array( $iFollowing, $aFollowers );

echo "$iFollowing: ".( $isFollowing ? 'OK' : '!!!' )."<br/>";

if( !$isFollowing )
{
$parameters = array( 'user_id' => $iFollowing );
$status = $oTwitter->post('friendships/destroy', $parameters);
} if ($i++ === 100 ) break;
}

会不会是其他问题?

编辑:在这篇文章中添加了自己的答案,代码可以在 twitter 上关注关注者和取消关注非关注者。

4

2 回答 2

7

这行得通,我想也许它可以帮助像我这样的新手。安迪琼斯的回答真的很有帮助。在这里也将很棒的图书馆用于许多其他事情。

  require_once 'twitteroauth.php';

 $oTwitter = new TwitterOAuth 
(   'YOUR_TWITTER_APP_CONSUMER_KEY',
'YOUR_TWITTER_APP_CONSUMER_SECRET',
'YOUR_TWITTER_APP_OAUTH_TOKEN',
'YOUR_TWITTER_APP_OAUTH_SECRET');
 

//带有光标的完整跟随者数组(跟随者> 5000)

$e = 1;
$cursor = -1;
$full_followers = array();
do {

$follows = $oTwitter->get("followers/ids.json?screen_name=yourusername&cursor=".$cursor);

$foll_array = (array)$follows;

  foreach ($foll_array['ids'] as $key => $val) {

        $full_followers[$e] = $val;
        $e++; 
  }
       $cursor = $follows->next_cursor;

  } while ($cursor > 0);
echo "Number of following:" .$e. "<br /><br />";

//带有光标的完整朋友数组(以下> 5000)

$e = 1;
$cursor = -1;
$full_friends = array();
do {

  $follow = $oTwitter->get("friends/ids.json?screen_name=yourusername&cursor=".$cursor);
  $foll_array = (array)$follow;

  foreach ($foll_array['ids'] as $key => $val) {

        $full_friends[$e] = $val;
        $e++;
  }
      $cursor = $follow->next_cursor;

} while ($cursor > 0);

//如果我关注用户并且他没有关注我,我取消关注他

    $index = 1;
    $unfollow_total=0;
    foreach( $full_friends as $iFollow )
    {
    $isFollowing = in_array( $iFollow, $full_followers );
     
    echo "$iFollow: ".( $isFollowing ? 'OK' : '!!!' )."<br/>";
    $index++;
     if( !$isFollowing )
        {
        $parameters = array( 'user_id' => $iFollow );
        $status = $oTwitter->post('friendships/destroy', $parameters);
        $unfollow_total++;
        } if ($unfollow_total === 5) break;
    }

//如果用户关注我而我不是,我关注

$index = 1;
$follow_total = 0;

foreach( $full_followers as $heFollows )
{
$amFollowing = in_array( $heFollows, $full_friends );
 
echo "$heFollows: ".( $amFollowing ? 'OK' : '!!!' )."<br/>";
 $index++;
 if( !$amFollowing )
    {
    $parameters = array( 'user_id' => $heFollows );
    $status = $oTwitter->post('friendships/create', $parameters);
    $follow_total++;
    } if ($follow_total === 5) break;
}

echo 'Unfollowed:'.$unfollow_total.'<br />';
echo 'Followed:'.$follow_total.'<br />';
于 2013-09-16T00:46:49.297 回答
6

如果您的追随者大于 5000,则$aFollowers = $oTwitter->get('followers/ids');只会返回前 5000 个 ID。以什么顺序?Twitter 不保证任何顺序,所以我们只是假设随机。

如果选择以下检查$isFollowing = in_array( $iFollowing, $aFollowers );,则此人$iFollowing可能在列表中,也可能不在列表中,$aFollowers具体取决于 Twitter 如何将关注者返回给您。如果此人在前 5000 人中,那么这将起作用,如果他们在前 5000 人之外,那么即使该人合法地关注您,检查也会失败。

您需要通过光标拉出所有关注者。查看游标/页面上的文档- 会对您有所帮助。基本上你需要这样做。

$aFollowers = array();
$cursor = -1;
do {
  $follows = $oTwitter->get('followers/ids?cursor=' . $cursor);
  $aFollowers = array_merge($follows->ids, $aFollowers);
  $cursor = $follows->next_cursor;
} while ($cursor > 0);
于 2013-09-14T23:21:35.697 回答