2012-06-20 65 views
2

我現在有幾千個Twitter追隨者,並且到現在爲止我已經回到手工跟了上去。我現在想用PHP自動化這個過程,因爲它可能需要很長時間才能追隨每個人。自動跟蹤回在Twitter上

我發現亞伯拉罕·威廉姆斯創建的PHP嘰嘰喳喳庫,並開始編寫一些代碼。

但是,每次我運行該腳本,我需要遵循回來的用戶數時間不正確!這是我編碼中的錯誤,還是Twitter API的工作原理?

這裏是我的代碼:

<?php 

require_once 'twitteroauth/twitteroauth.php'; 

define('CONSUMER_KEY', ''); 
define('CONSUMER_SECRET', ''); 
define('ACCESS_TOKEN', ''); 
define('ACCESS_TOKEN_SECRET', ''); 

ob_start(); 
set_time_limit(0); 

function autoFollow($action){ 
    //auth with twitter. 
    $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); 

    //get the last 5000 followers 
    $followers = $toa->get('followers/ids', array('cursor' => -1)); 
    $followerIds = array(); 

    foreach ($followers->ids as $i => $id) { 
     $followerIds[] = $id; 
    } 

    //get the last 5000 people you've followed 
    $friends = $toa->get('friends/ids', array('cursor' => -1)); 
    $friendIds = array(); 
    foreach ($friends->ids as $i => $id) { 
     $friendIds[] = $id; 
    } 


    if($action=="unfollow"){ 
     //unfollow all users that aren't following back. 
     $usersNotFollowingBackcount = 0; 
     $usersNotFollowingBack = array(); 

     foreach($friendIds as $id){ 
      if(!in_array($id,$followerIds)){ 
       array_push($usersNotFollowingBack, $id); 
       //unfollow the user 
       //$toa->post('friendships/destroy', array('id' => $id)); 
       $usersNotFollowingBackcount++; 
       echo $usersNotFollowingBackcount.' users unfollowed so far</br>'; 
       ob_flush(); 
       flush(); 
      } 
     } 

     echo sizeof($usersNotFollowingBack).' users who weren\'t following you back have now been unfollowed!'; 
    } 
    if($action =="follow"){     
     //follow all users that you're not following back. 
     $usersYoureNotFollowingBackcount = 0; 
     $usersYoureNotFollowingBack = array(); 

     foreach($followerIds as $id){ 
      if(!in_array($id,$friendIds)){ 
       array_push($usersYoureNotFollowingBack, $id); 
       //follow the user 
       //$toa->post('friendships/create', array('id' => $id)); 
       $usersYoureNotFollowingBackcount++; 
       echo $usersYoureNotFollowingBackcount.' users followed back so far</br>'; 
       ob_flush(); 
       flush(); 
      } 
     } 

     echo sizeof($usersYoureNotFollowingBack).' users have been followed back!'; 
    } 
} 

if($_GET['action']){ 
    autoFollow($_GET['action']); 
    ob_end_flush(); 
} 
?> 
+4

爲什麼要跟着那麼多的人,它甚至成爲這樣的一個苦差事,你必須自動化它? – BoltClock

+2

然後,您需要一個腳本來阻止所有來自您所關注的人的傳入推文。我在Twitter上追蹤用戶的 –

+0

80%是朋友,所以我不介意看到我的時間表:) –

回答

8

我剛剛與我的Twitter帳戶運行在我的筆記本電腦您的代碼和我得到正確的價值觀。你的代碼在你的身邊是正確的。

如果追隨者或追隨者人數超過5000人或他們之間的差異超過5000人,則存在不一致的可能性,因爲某些用戶不會顯示在最近的5000個追隨者中,因爲在他之後,其他5000個用戶跟着你,你從未跟着他。所以這個角色會被錯過。

如果你的問題是在運行的代碼,有些用戶沒有得到遵循,也可能是因爲Twitter的API速率限制。

OAuth調用每小時允許350個請求,並根據請求中使用的oauth_token進行度量。

檢查這更多:https://dev.twitter.com/docs/rate-limiting 所以以下超過350個用戶將不可能由於利率由Twitter的限制。

+0

至少在一小時內超過350是不可能的。顯然,要遵循的用戶可以按API允許的速度排隊並提交。 – quickshiftin

+0

@quickshiftin是的,但OP不在這裏回答他在做什麼。 :\ – ksg91

+0

Hi @ ksg91。感謝您的回答。對不起,我現在只是在回覆你,但在過去的12天裏我已經離開了。 http://ifttt.com/似乎是我的問題的最佳解決方案,但無論如何感謝您的答覆。 –

1

這工作遵循/與twitteroauth.php + OAuth.php和APPI V1.1取消關注,如果你有超過5000追隨者/朋友。後續取消關注的999限制是因爲1000天的限制。我開始with this

//FULL FOLLOWERS ARRAY WITH CURSOR (FOLLOWERS > 5000) 
    $e = 0; 
    $cursor = -1; 
    $full_followers = array(); 
    do { 
     //SET UP THE URL 
     $follows = $oTwitter->get("followers/ids.json?screen_name=youruser&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 followers:" .$e. "<br /><br />"; 

//FULL FRIEND ARRAY WITH CURSOR (FOLLOWING > 5000) 
    $e = 0; 
    $cursor = -1; 
    $full_friends = array(); 
    do { 

     $follow = $oTwitter->get("friends/ids.json?screen_name=youruser&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); 
    echo "Number of following:" .$e. "<br /><br />"; 

//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 $index .":"."$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 />';