PHP/cURL Cookie的替代方案


Alternatives for PHP / cURL Cookies?

是否有任何替代方案在服务器上使用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