2013-04-02 89 views
1

我想從腳本中產生的兩個並行cURL請求的頭信息中返回兩個http狀態碼。到目前爲止,我的腳本如下所示,最後print_r()打印出來:Array([0] => 200 [1] =>)。我不確定爲什麼它不返回第二個狀態碼?提前致謝。從PHP中的多個cURL返回http狀態碼

function checkHTTPStatusCode($ip1,$ip2) { 
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"; 

    //create cURL resources 
    $ch1 = curl_init(); 
    $ch1 = curl_init(); 

    //set opptions 
    curl_setopt ($ch1, CURLOPT_URL,$ip1); 
    curl_setopt ($ch1, CURLOPT_USERAGENT, $agent); 
    curl_setopt ($ch1, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch1, CURLOPT_VERBOSE, false); 
    curl_setopt ($ch1, CURLOPT_TIMEOUT, 5); 

    curl_setopt ($ch2, CURLOPT_URL,$ip2); 
    curl_setopt ($ch2, CURLOPT_USERAGENT, $agent); 
    curl_setopt ($ch2, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch2, CURLOPT_VERBOSE, false); 
    curl_setopt ($ch2, CURLOPT_TIMEOUT, 5); 

    //create the multiple cURL handle 
    $mh = curl_multi_init(); 

    //add the two handles 
    curl_multi_add_handle($mh,$ch1); 
    curl_multi_add_handle($mh,$ch2); 

    //execute the handles 
    $running = null; 
    do { 
     curl_multi_exec($mh, $running); 
    } while($running > 0); 

    //get http status codes 
    $httpcode1 = curl_getinfo($ch1, CURLINFO_HTTP_CODE); 
    $httpcode2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE); 

    //close the handles 
    curl_multi_remove_handle($mh, $ch1); 
    curl_multi_remove_handle($mh, $ch2); 
    curl_multi_close($mh); 

    return array($httpcode1,$httpcode2); 
} 

$test = checkHTTPStatusCode('http://www.yahoo.com','http://www.google.com'); 

print_r($test); 

回答

1

錯字這裏:

//create cURL resources 
$ch1 = curl_init(); 
$ch1 = curl_init(); 

我猜你的第二個意思$ch2 = curl_init();

+0

謝謝!是肯定感到愚蠢的,現在不注意到一個錯字:( –