2012-06-25 90 views
0

我已經編寫了以下代碼來檢索特定用戶的超過100個Twitter追隨者。使用POST獲取Twitter追隨者

<?php 
$cursor = -1; 
$account_from = 'twitterapi'; 
do 
{ 
$json = file_get_contents('http://api.twitter.com/1/followers/ids.json?cursor='.$cursor.'&screen_name='.$account_from.''); 

$accounts = json_decode($json); 


foreach ($accounts->ids as $account) 
{ 

     $a[] = $account ; 


} 
$cursor = $accounts->next_cursor; 


} 
while ($cursor > 0); 


    $n = ceil(count($a)/100) ; 
    $b = array_chunk($a, 100) ; 


    for($i=0 ; $i<$n ; $i++) { 

     $user = ''; 

     for($j=0 ; $j<count($b[$i]) ; $j++) { 

        $user = $user.$b[$i][$j].',' ; 


     } 

     $json=file_get_contents('http://api.twitter.com/1/users/lookup.json?user_id='.$user.'') ; 
     $fo= json_decode($json); 

     foreach ($fo as $key => $jsons) { 

      foreach($jsons as $key => $value) { 

        if($key == 'screen_name'){ 

          $arr[] = $value; 

        } 

      } 

     } 

    } 

    return $arr ; 



?> 

但正如你所看到的,也有在HTTP請求太多的瓶頸,有時如果用戶有很多的追隨者,該腳本將由於PHP的最大執行限制超時。

但是根據this,我們應該使用POST來處理更大的請求。我不知道如何使用POST代替。

任何幫助,非常感謝。

回答

0

使用file_get_contents,您無法發送POST請求。我建議使用Guzzle,這本質上是一個乾淨的OOP包裝紙卷繞。 'tour'頁面上的Twitter API甚至有一個例子:here

+0

您可以使用file_get_contents發送POST請求:'file_get_contents($ url,NULL,stream_context_create(array('http'=> array('method'= >'POST','content'=> http_build_query($ data)))))' –

+0

@AlfEaton是的,在寫這個答案的時候,我一定有一個大腦放屁。 – Lusitanian

相關問題