我正在尝试从多个网页中获取标头信息。我尝试使用以下代码使用单个cURL请求来执行此操作:
<?php
$arr = array(
"John", "Mary",
"William", " Peter",
"James", "Emma",
"George", "Elizabeth",
"Charles", "Margaret",
);
$ch = curl_init();
for($i=0; $i<sizeOf($arr); $i++){
$url = "https://example.com/".$arr[$i];
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo $arr[$i]." Error<br>";
} else {
echo $arr[$i]." Success<br>";
}
}
curl_close($ch);
?>
但是这段代码似乎需要很长的执行时间。我搜索了互联网&amp;找到了 curl_multi_exec ,可以用来一次运行多个cURL请求。所以现在我使用这段代码:
<?php
ini_set('max_execution_time', 0);
$arr = array(
"John", "Mary",
"William", " Peter",
"James", "Emma",
"George", "Elizabeth",
"Charles", "Margaret",
);
function multiRequest($data) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = "https://example.com/".$data[$id];
$options = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
);
// extra options?
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
//Code to fetch header info
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
return $result;
}
multiRequest($arr);
?>
如何从curl_multi_init HTTP请求中获取多个header_info?
答案 0 :(得分:0)
第一个例子中的代码:
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $httpCode != 200 ){
echo $arr[$i]." Error<br>";
} else {
echo $arr[$i]." Success<br>";
}
即使curl句柄由curl_multi_exec()
执行,也能正常工作。
在第二个示例中,替换此代码:
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
//Code to fetch header info
curl_multi_remove_handle($mh, $c);
}
用这个:
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
$httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
$url = curl_getinfo($c, CURLINFO_EFFECTIVE_URL);
if ( $httpCode != 200 ){
echo $url." Error<br>";
} else {
echo $url." Success<br>";
}
curl_multi_remove_handle($mh, $c);
}