PHP / cURL Cookies的替代品?

时间:2015-06-22 12:39:15

标签: php caching curl cookies

是否任何替代,PHP / cURL 没有服务器上的Cookie缓存,比如在cURL选项CURLOPT_COOKIEJAR和CURLOPT_COOKIEFILE中使用.txt文件?

我尝试从CURL会话的HTTP标头中读取cookie并手动设置它们以避免在此处进行CURL会话的服务器端存储。

1 个答案:

答案 0 :(得分:0)

您可以直接设置标题。

$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