dtf76989 2013-09-14 06:28
浏览 20
已采纳

PHP TWITTER bot使用api 1.1版和游标来关注/取消关注

This code should only unfollow users who are not following back, but it unfollows SOME followers, cannot figure out why.

$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;
}

Could it be that the problem is something else?

EDIT: Added own answer to this post with code that works to follow followers and unfollow non-followers on twitter.

  • 写回答

2条回答 默认 最新

  • douhuan1905 2013-09-14 23:21
    关注

    If your of followers is greater than 5000 then $aFollowers = $oTwitter->get('followers/ids'); will only return the first 5000 ids. In what order? Twitter does not guarantee any order, so we'll just assume random.

    If the following check $isFollowing = in_array( $iFollowing, $aFollowers );, the person $iFollowing may or may not be in the list $aFollowers depending on how Twitter returned the followers to you. If the person is in the first 5000, then this will work, if they're outside the first 5000 then the check will fail, even if the person is legitimately following you.

    You'll need to pull all your followers via cursors. Check out the doc on cursors / pages - will help you out a bit. Basically you need to do this.

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?