PHP 使用 curl 获取响应头的2种方法
PHP 使用 curl
类库,获取请求的回应头(Response Headers),下面总结了2种方法。
1. 使用 CURLOPT_HEADER 选项
设置选项 curl_setopt()
方法,当 CURLOPT_HEADER
设置 true
,curl_exec
结果将输出回应头。这时如果 CURLOPT_NOBODY
设置为 false
,curl_exec
将返回回应头和内容 body,否则只返回回应头。
curl_getinfo($ch, CURLINFO_HEADER_SIZE)
返回回应头的字符长度,方便将 header 和 body 区分出来。
$url = "https://www.baidu.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// 1 表示只返回请求头
curl_setopt($ch, CURLOPT_NOBODY, 0);
$response = curl_exec($ch);
// curl_exec 之后
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
echo $header;
// echo $body;
输出
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 14722
Content-Type: text/html
Date: Thu, 23 Apr 2020 15:02:52 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM "
P3p: CP=" OTI DSP COR IVA OUR IND COM "
Pragma: no-cache
Server: BWS/1.1
Set-Cookie: BAIDUID=2075A60690A5E152C301511B9CA6982A:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=2075A60690A5E152C301511B9CA6982A; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1587654172; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=2075A60690A5E1527030AB064F519CA8:FG=1; max-age=31536000; expires=Fri, 23-Apr-21 15:02:52 GMT; domain=.baidu.com; path=/; version=1; comment=bd
Traceid: 1587654172022180301811607925382683554957
Vary: Accept-Encoding
X-Ua-Compatible: IE=Edge,chrome=1
2. 使用 CURLOPT_HEADERFUNCTION 选项
CURLOPT_HEADERFUNCTION
选项,设置一个回调函数,这个函数有两个参数,第一个是cURL的资源句柄,第二个是输出的 header 数据。header数据的输出必须依赖这个函数,返回已写入的数据大小。
$headers = [];
$url = "https://www.baidu.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function ($curl, $header) use (&$headers) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
$response = curl_exec($ch);
print_r($headers);
将输出
Array
(
[accept-ranges] => Array
(
[0] => bytes
)
[cache-control] => Array
(
[0] => no-cache
)
[connection] => Array
(
[0] => keep-alive
)
[content-length] => Array
(
[0] => 14722
)
[content-type] => Array
(
[0] => text/html
)
[date] => Array
(
[0] => Thu, 23 Apr 2020 15:01:42 GMT
)
[p3p] => Array
(
[0] => CP=" OTI DSP COR IVA OUR IND COM "
[1] => CP=" OTI DSP COR IVA OUR IND COM "
)
[pragma] => Array
(
[0] => no-cache
)
[server] => Array
(
[0] => BWS/1.1
)
[set-cookie] => Array
(
[0] => BAIDUID=C0D84316D0C85F1E22A6685A73ADE4BE:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[1] => BIDUPSID=C0D84316D0C85F1E22A6685A73ADE4BE; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[2] => PSTM=1587654102; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[3] => BAIDUID=C0D84316D0C85F1EF148BCBDC9AEE4EB:FG=1; max-age=31536000; expires=Fri, 23-Apr-21 15:01:42 GMT; domain=.baidu.com; path=/; version=1; comment=bd
)
[traceid] => Array
(
[0] => 1587654102022199962611480622685312756450
)
[vary] => Array
(
[0] => Accept-Encoding
)
[x-ua-compatible] => Array
(
[0] => IE=Edge,chrome=1
)
)
非特殊说明,本网站所有文章均为原创。如若转载,请注明出处:https://mip.cpming.top/p/get-response-header-from-php-curl