PHP TWITTER bot to follow/unfollow using api version 1.1 and cursors

前端 未结 2 999
粉色の甜心
粉色の甜心 2021-02-04 19:56

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

$oTwitter = new TwitterOAuth (...)

$aFo         


        
2条回答
  •  庸人自扰
    2021-02-04 20:39

    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);
    

提交回复
热议问题