此代码应仅取消关注未跟踪的用户,但取消关注某些关注者,无法找出原因。
$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上取消关注非关注者。
答案 0 :(得分:7)
这可行,我想也许它可以帮助像我这样的新手。安迪琼斯的回答真的很有帮助。对于许多其他事情也使用了很棒的库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);
//如果我跟随用户并且他没有回到我身边,我就不知道了 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 === 999) 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 === 999) break;
}
echo 'Unfollowed:'.$unfollow_total.'<br />';
echo 'Followed:'.$follow_total.'<br />';
答案 1 :(得分:6)
如果您的粉丝大于5000,那么$aFollowers = $oTwitter->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);