【问题标题】:PHP TWITTER bot to follow/unfollow using api version 1.1 and cursorsPHP TWITTER bot 使用 api 1.1 版和游标来关注/取消关注
【发布时间】:2013-09-18 21:23:45
【问题描述】:

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

$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 上关注关注者和取消关注非关注者的代码为这篇文章添加了自己的答案。

【问题讨论】:

  • 你在关注/你有超过 5000 个关注者吗?这些 API 调用的 id 上限为 5000,您需要在此之外使用分页。

标签: php twitter oauth


【解决方案1】:

这行得通,我想也许它可以帮助像我这样的新手。安迪琼斯的回答真的很有帮助。还为许多其他事情使用了很棒的库here

  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 />';

【讨论】:

  • 限制是999?像这样的推特?保守点不应该是 10 或 20 之类的吗?
  • 是的,这个答案是 Twitter 在过去狂野西部的美好时光,我们可以在没有审查的情况下做到这一点。现在一天要非常小心......如果您的帐户是新的,关注者很少,请格外小心,因为 twitter 可能会立即禁止您的帐户。提示,根据我的经验,自动取消关注不像自动关注那么严重
  • 如果有人按原样复制粘贴,我将编辑答案...请记住,这是 2013 年的脚本,当您批量关注超过 999 个用户时被禁止。今天这个脚本可以工作,但我猜你最多第二天就会失去你的帐户。
  • 是的,这里是新的限制help.twitter.com/en/rules-and-policies/twitter-limits,请注意激进的术语。
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多