PHP Curl multi-exec请求不返回所有数据

时间:2011-12-14 11:33:21

标签: php twitter curl

我需要一些关于调试下面代码的建议。

我从$ userIDArray中提取Twitter用户ID。由于Twitter的限制,我必须将调用分成100个批次。因此,如果$ userIDarray包含512个用户,我使用curl_multi_exec进行6个同时调用。

当我查看返回数据时,第一批100个响应总是很好,然后我得到的批次只返回0和1个结果。因此,从512个用户中我可能只获得120的返回信息。

我如何知道这些电话会发生什么?

function getUserInfo()
{
    global $userIDArray;
    global $counter;
    global $userInfoArray;
    $handleArray = array();

    $requiredCalls = ceil($counter / 100);
    echo "Calls ".$requiredCalls."</br>";

    $mh = curl_multi_init();

    for($i = 0; $i < $requiredCalls; $i++) 
    {
        $counterLow = $counter - 100;

        if($counterLow < 0)
        {
            $counterLow = 0;    
        }

        //Take only 100 items
        $outputUIDArray = array_slice($userIDArray,$counterLow, $counter);

        //Implode array to string of user ids
        $uids = implode(",", $outputUIDArray);
        //echo "UIDs = ".$uids;

        $handle=curl_init();
        curl_setopt($handle,CURLOPT_URL,'https://api.twitter.com/1/users/lookup.json?user_id='.$uids.'&include_entities=false');
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);


        $handleArray[] = $handle;

        curl_multi_add_handle($mh,$handleArray[$i]);

        echo "Counter low ".$counterLow." Counter high ".$counter."</br>";

        $counter -= 100;
    }

    $active = null;
    //execute the handles
    do {
        $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }


    for($i = 0; $i < $requiredCalls; $i++) 
    {   
        //Get result
        $result = curl_multi_getcontent ($handleArray[$i]);
        //echo( $i . "\n" . $results . "\n");

        $json_a=json_decode($result,true);
        echo count($json_a)."</br>";
        //print_r($json_a);

        for($j = 0; $j < count($json_a); $j++)
        {
            $userInfoArray[] = $json_a[$j];
        }

        var_dump(curl_multi_info_read($mh));
        echo"</br>";

        //close the handles
        curl_multi_remove_handle($mh, $handleArray[$i]);
    }


    curl_multi_close($mh);  

    echo "Results in final array ".count($userInfoArray);}

1 个答案:

答案 0 :(得分:2)

我误解了数组切片是如何工作的

$outputUIDArray = array_slice($userIDArray,$counterLow, $counter);

应该是

$outputUIDArray = array_slice($userIDArray,$counterLow, 100);