使用POST获取Twitter粉丝

时间:2012-06-25 08:30:56

标签: php twitter

我编写了以下代码,用于检索特定用户的超过100个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代替。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

使用cURL extension。在curl_setopt中将CURLOPT_POST设置为TRUE。页面的示例部分中有一些示例

答案 1 :(得分:0)

使用file_get_contents,您无法发送POST请求。我建议使用Guzzle,它本质上是一个围绕cURL的干净OOP包装器。在“旅游”页面上有一个Twitter API的例子:here

相关问题