Curl有一个超时函数,可以用它来获取头文件。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.example.com/file.pdf");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
// The maximum number of seconds to allow cURL functions to execute.
curl_setopt($ch, CURLOPT_TIMEOUT, 20); //timeout in seconds
$output = curl_exec($ch);
// close handle
curl_close($ch);
$headers = [];
$data = explode("\n",$output);
$headers['status'] = $data[0];
array_shift($data);
foreach($data as $part){
$middle=explode(":",$part);
$headers[trim($middle[0])] = trim($middle[1]);
}
echo "<pre>";
var_dumo(headers);
岗位代码
how to set curl timeout
和
get header from php curl