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

前端 未结 2 987
粉色の甜心
粉色の甜心 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 20:24

    This works, I think maybe it can help novices like me. Andy Jones answer really helped. Used great library for many other things as well 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. "

    ";

    //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' : '!!!' )."
    "; $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' : '!!!' )."
    "; $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.'
    '; echo 'Followed:'.$follow_total.'
    ';

提交回复
热议问题