这行得通,我想也许它可以帮助像我这样的新手。安迪琼斯的回答真的很有帮助。在这里也将很棒的图书馆用于许多其他事情。
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 />';