PHP TWITTER 机器人使用 api 版本 1.1 和游标来关注/取消关注

标签 php twitter oauth

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

$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 上关注关注者和取消关注非关注者的代码。

最佳答案

这个可行,我想也许可以帮助到像我这样的新手。安迪琼斯的回答真的很有帮助。也将伟大的图书馆用于许多其他事情 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');
 

//FULL FOLLOWERS ARRAY WITH CURSOR ( FOLLOWERS > 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 />";

//FULL FRIEND ARRAY WITH CURSOR (FOLLOWING > 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);

//IF I AM FOLLOWING USER AND HE IS NOT FOLLOWING ME BACK, I UNFOLLOW HIM

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

//IF USER IS FOLLOWING ME AND I AM NOT, I FOLLOW

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

关于PHP TWITTER 机器人使用 api 版本 1.1 和游标来关注/取消关注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799023/

相关文章:

java - 读取UTF超时

oauth - 如何在 postman 收藏中保留OAuth2 token (或使用刷新 token )?

php - 带有 preg_match 的有效电子邮件

php - 如何在yii中上传照片?

php - Wordpress get_results from mysql where string 没有给出结果

php - preg 替换 twitter 状态和 http 或 https PHP

php - 如何使用 twitter 1.1 api 和 twitteroauth 发布推文

iphone - magento REST API 在 iPhone 中无法访问

authentication - oauth 与 authsub

php - option select by based on the value from db with php 选项