2011-12-14 75 views
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);} 

回答

2

我被误解阵列片是如何工作的

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

应该

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