1
我需要一些關於調試下面代碼的建議。PHP 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);}