在curl PHP中获取请求头中的Cookie,可以通过以下步骤实现:
以下是一个示例代码:
<?php
function headerCallback($curl, $header_line) {
if (strpos($header_line, "Set-Cookie") !== false) {
$cookie = trim(explode(":", $header_line)[1]);
$cookies[] = $cookie;
}
return strlen($header_line);
}
$ch = curl_init();
$url = "http://example.com";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "headerCallback");
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$headers = $info["request_header"];
$cookies = isset($cookies) ? $cookies : [];
echo "请求头信息:\n";
echo $headers . "\n";
echo "Cookie:\n";
foreach ($cookies as $cookie) {
echo $cookie . "\n";
}
?>
这段代码使用curl库发送一个GET请求,并在请求头中获取Cookie信息。最后,将请求头信息和Cookie打印出来。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云