使用api版本1.1和游标跟踪/取消跟踪PHP TWITTER bot

使用api版本1.1和游标跟踪/取消跟踪PHP TWITTER bot,php,twitter,oauth,Php,Twitter,Oauth,这段代码应该只对没有跟随的用户进行解锁,但对一些跟随者进行解锁,无法找出原因 $oTwitter = new TwitterOAuth (...) $aFollowing = $oTwitter->get('friends/ids'); $aFollowing = $aFollowing->ids; $aFollowers = $oTwitter->get('followers/ids'); $aFollowers = $aFollowers->ids; $i=1;

这段代码应该只对没有跟随的用户进行解锁,但对一些跟随者进行解锁,无法找出原因

$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;
}
$oTwitter=new TwitterOAuth(…)
$aFollowing=$oTwitter->get('friends/id');
$A跟踪=$A跟踪->ID;
$aFollowers=$oTwitter->get('followers/id');
$aFollowers=$aFollowers->ids;
$i=1;
foreach($a以下为$i以下)
{
$isFollowing=在数组中($iffollowing,$affollowers);
回声“$iFollowing:”($iFollowing?'OK':'!!!”)“
”; 如果(!$isFollowing) { $parameters=数组('user\u id'=>$ifollow); $status=$oTwitter->post('friendships/destroy',$parameters); }如果($i++==100)中断; }
可能是别的问题吗

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

如果您的追随者数量大于5000,则
$affollowers=$oTwitter->get('followers/id')将仅返回前5000个ID。按什么顺序?推特不保证任何订单,所以我们假设是随机的

如果以下检查
$isFollowing=在数组中($iffollowing,$affollowers)
,根据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);

这很管用,我想也许它可以帮助像我这样的新手。安迪·琼斯的回答真的很有帮助。在许多其他方面也使用了大图书馆

//带光标的完整跟随者阵列(跟随者>5000)

//若我在跟踪用户,而他并没有跟踪我,我将取消跟踪 他

$index=1;
$unfollow_总计=0;
foreach($iFollow为完整的朋友)
{
$isfollow=in_数组($iffollow,$full_followers);
echo“$iFollow:”($isFollowing'OK':'!!!”)“
”; $index++; 如果(!$isFollowing) { $parameters=数组('user\u id'=>$iFollow); $status=$oTwitter->post('friendships/destroy',$parameters); $unfollow_总计++; }如果($unfollow_total==5)中断; }
//如果用户在跟踪我,而我没有,我就跟踪

$index=1;
$follow_总计=0;
foreach($hefollowers$hefollowers)
{
$amfollow=在数组中($heFollows,$full\u friends);
回声“$heFollows:”($amFollowing?'OK':'!!!”)“
”; $index++; 如果(!$amFollowing) { $parameters=数组('user_id'=>$HEF); $status=$oTwitter->post('friendships/create',$parameters); $follow_total++; }如果($follow_total==5)中断; } 回音“unfollow:”。$unfollow_总计。
; echo“follow:”.$follow_总计。“
”;
您是否有追随者/您是否有超过5000名追随者?这些API调用的id上限为5000,您需要使用超过该上限的分页。限制为999?像那样推特?保守一点,不应该是10或20岁吗?是的,这个答案是在推特在荒野西部的好时光,我们可以在没有审查的情况下做到这一点。现在一天要非常小心。。。此外,如果你的帐户是新的,没有几个追随者,要格外小心,因为twitter可能会立即禁止你的帐户。提示,根据我的经验,自动取消跟踪并不像自动跟踪那样被严格禁止。我会编辑答案,以防有人复制粘贴。。。请记住,这是一个2013年的脚本,当你批量跟踪超过999个用户时被禁止。今天这个脚本可以运行,但我想你最多第二天就会失去你的帐户。是的,这里是新的限制,请注意这个激进的术语。
  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');
 
$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 />";
$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 />';