使用 curl_getinfo(),您可以獲取請求的回應代碼: https ://www.php.net/manual/en/function.curl-getinfo.php
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE));
有函式 curl_multi_info_read(),但我認為它似乎并沒有做同樣的事情: https ://www.php.net/manual/en/function.curl-multi-info-read.php
Contents of the returned array
Key: Value:
msg The CURLMSG_DONE constant. Other return values are currently not available.
result One of the CURLE_* constants. If everything is OK, the CURLE_OK will be the result.
handle Resource of type curl indicates the handle which it concerns.
代碼示例:
var_dump(curl_multi_info_read($mh));
給出如下輸出:
array(3) {
["msg"]=>
int(1)
["result"]=>
int(0)
["handle"]=>
resource(5) of type (curl)
}
而不是給出 HTTP 回應代碼。有沒有辦法從這個回傳的陣列中獲取 HTTP 回應代碼?或者 curl_multi() 中的其他方式來獲取回應代碼?
uj5u.com熱心網友回復:
您可以使用curl_multi_select() ,如curl_multi_info_read ()中的第一個示例所示。然后您可以使用它$info['handle']來獲取有關所有請求的資訊。
$urls = [
'http://www.cnn.com/',
'http://www.bbc.co.uk/',
'http://www.yahoo.com/'
];
$codes = [];
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
$conn[$i] = curl_init($url);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $conn[$i]);
}
do {
$status = curl_multi_exec($mh, $active);
if ($active) {
curl_multi_select($mh);
}
while (false !== ($info = curl_multi_info_read($mh))) {
//
// here, we can get informations about current handle.
//
$url = curl_getinfo($info['handle'], CURLINFO_REDIRECT_URL);
$http_code = curl_getinfo($info['handle'], CURLINFO_HTTP_CODE);
// Store in an array for future use :
$codes[$url] = $http_code;
}
} while ($active && $status == CURLM_OK);
foreach ($urls as $i => $url) {
// $res[$i] = curl_multi_getcontent($conn[$i]);
curl_close($conn[$i]);
}
// display results
print_r($codes);
輸出 :
Array
(
[https://www.cnn.com/] => 301
[https://www.bbc.co.uk/] => 302
[https://www.yahoo.com/] => 301
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/422922.html
標籤:
上一篇:為什么我的TwitteroAuth請求不起作用?|卷曲,標題
下一篇:無法將值變數轉換為CurlPhp
