【发布时间】:2015-06-22 12:39:14
【问题描述】:
是否有 任何替代方法使用 PHP / cURL 服务器上没有 cookie 缓存,例如在 cURL 选项 CURLOPT_COOKIEJAR 和 CURLOPT_COOKIEFILE 中使用 .txt 文件?
我尝试从 CURL 会话的 HTTP 标头中读取 cookie 并手动设置它们以避免 CURL 会话的服务器端存储。
【问题讨论】:
是否有 任何替代方法使用 PHP / cURL 服务器上没有 cookie 缓存,例如在 cURL 选项 CURLOPT_COOKIEJAR 和 CURLOPT_COOKIEFILE 中使用 .txt 文件?
我尝试从 CURL 会话的 HTTP 标头中读取 cookie 并手动设置它们以避免 CURL 会话的服务器端存储。
【问题讨论】:
你可以直接设置标题。
$cookies = array(
'somekey' => 'somevalue'
);
$endpoint = 'https://example.org';
$requestMethod = 'POST';
$timeout = 30;
$headers = array(
sprintf('Cookie: %s', http_build_query($cookies, null, '; '))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // you may need to make this false depending on the servers certificate
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestMethod);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$response = curl_exec($ch);
list($header, $body) = explode("\r\n\r\n", $response, 2);
// now all the headers from your request will be available in $header
【讨论】: