可以使用 curl_setopt() 函数来设置 cURL 请求的请求头。
$curl = curl_init('https://example.com');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer my-token'
));
$response = curl_exec($curl);
curl_close($curl);
以上代码将 cURL 请求的 Content-Type 头设置为 application/json,并将 Authorization 头设置为 Bearer my-token 。
你也可以使用 curl_setopt() 函数的 CURLOPT_HEADER 选项来获取响应头。
$curl = curl_init('https://example.com');
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
$response = curl_exec($curl);
curl_close($curl);
$headers = explode("\n", $response);
foreach ($headers as $header) {
echo $header . PHP_EOL;
}
以上代码将获取https://example.com 的响应头,并将其打印出来。
|