我试图将CURL请求发送到URL,然后获取此请求的cookie,然后在另一个CURL请求中使用它们。
我目前正在获得以下cookie:
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"USER" => "ABC",
"PASSWORD" => "123"
));
$result = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
curl_close($ch);
现在,我想在同一PHP脚本中将$cookies
数组的cookie用于另一个CURL请求...我试图那样做,但是完全不正确:
$ch2 = curl_init('https://example.com/2');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HEADER, 1);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
foreach($cookies as $s) {
"Cookie: ".$s
}
));
$result2 = curl_exec($ch2);
curl_close($ch2);
任何帮助将不胜感激。
谢谢!