2014-10-29 25 views
0

我試圖實現一些多個捲曲函數,而不是簡單的捲曲函數。將腳本從cURL重建爲多個捲曲

我有下面的代碼片段:

$curl = curl_init(); 
     curl_setopt($curl, CURLOPT_ENCODING,'gzip'); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl, CURLOPT_URL, $url); 
     $result = curl_exec($curl); 
     curl_close($curl); 
     $rv = ($returnArray) ? json_decode($result, true) : json_decode($result); 

它給我的結果($結果回報的成功和一些數據)。我想重寫它以使用curl_multi_init()。我試過這個:

$curl = curl_init(); 
     curl_setopt($curl, CURLOPT_ENCODING,'gzip'); 
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($curl, CURLOPT_URL, $url); 
     $mh = curl_multi_init(); 
     curl_multi_add_handle($mh,$curl); 


     $running= \null; 
     do { 
      curl_multi_exec($mh,$running); 
      $result = curl_multi_exec($mh,$running); 
     } while($running > 0); 
     curl_multi_remove_handle($mh, $curl); 

     curl_multi_close($mh); 
     $rv = ($returnArray) ? json_decode($result, true) : json_decode($result); 

我得不到結果($ result爲空)。我沒有任何錯誤。哪裏不對?

+0

RTM:http://php.net/manual/en/function.curl-multi-exec.php - 'curl_multi_exec()'返回'INT '不是結果數組 – AeroX 2014-10-29 14:11:19

回答

0

這個工作對我來說:

function get_result($nodes) 
    { 
     $node_count = count($nodes); 

     $curl_arr = array(); 
     $master = curl_multi_init(); 

     for($i = 0; $i < $node_count; $i++) 
     { 
      $url = $nodes[$i]; 
      $curl_arr[$i] = curl_init($url); 
      curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($curl_arr[$i], CURLOPT_CONNECTTIMEOUT, 10); 
      curl_setopt($curl_arr[$i], CURLOPT_TIMEOUT, 10); 
      curl_setopt($curl_arr[$i], CURLOPT_ENCODING, "gzip"); 
      curl_setopt($curl_arr[$i], CURLOPT_VERBOSE, true); 
      curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, 'Mozilla/5.0'); 
      curl_setopt($curl_arr[$i], CURLOPT_FOLLOWLOCATION, true); 
      curl_setopt($curl_arr[$i], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
      curl_multi_add_handle($master, $curl_arr[$i]); 
     } 

     do { 
      curl_multi_exec($master,$running); 
      curl_multi_select($master, 5.0); 
     } while($running > 0); 


     $output = ""; 
     for($i = 0; $i < $node_count; $i++) 
     { 
      $output .= curl_multi_getcontent($curl_arr[$i] ) . "<breaktag>"; 
      curl_multi_remove_handle($master, $curl_arr[$i]); 
      curl_close($curl_arr[$i]); 
     } 

     curl_multi_close($master); 

     return $output; 
    } 


    $nodes[] =() // your URLs 
    $responses = get_result($nodes); 
    $responses = explode("<breaktag>", $responses); //now responses is array of result 
+0

感謝您的答覆,但我不能使用這種方法。我必須將curl_multi_exec($ mh,$ running)嚴格分配給$ result變量。我不能去太野生:( – Agrrr 2014-10-29 13:55:18