PHP TWITTER bot 使用 api 版本 1.1 和游标来关注/取消关注 [英] PHP TWITTER bot to follow/unfollow using api version 1.1 and cursors

查看:50
本文介绍了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;
}

会不会是其他问题?

为这篇文章添加了自己的答案,代码可以在 Twitter 上关注关注者和取消关注非关注者.

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

推荐答案

如果你的粉丝超过 5000 那么 $aFollowers = $oTwitter->get('followers/ids');只会返回前 5000 个 ID.按什么顺序?Twitter 不保证任何顺序,所以我们只假设随机.

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.

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

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

这篇关于PHP TWITTER bot 使用 api 版本 1.1 和游标来关注/取消关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆