如何通过变量的 curl 请求存储我的 cookie

How to stock my cookies from a curl request on a variable

我试图阻止 CURL 将 cookie 会话存储在文件中。但是我需要这些 cookie 来进行第二次请求。所以我尝试了一个解决方案来获取变量中的 cookie,但是当我使用 "CURLOPT_HEADER true" 时,我没有从我需要的 Web 服务中得到答案。你知道我该怎么做吗,或者我是否可以将我的 cookie 存储在文件以外的文件中?

我用它来获取变量上的 cookie。

how to get the cookies from a php curl into a variable

所以我希望得到一个包含 Web 服务答案和我可以重复使用的 cookie 的响应(我认为 CURLOPT_COOKIE)。

谢谢

我通过对请求结果使用正则表达式解决了我的问题。并使用 CURLOPT_COOKIE.

// Execute the request
$response = curl_exec($ch);
// Extract the cookie from the answer
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $response, $matches);
$cookies = $matches[1][0];
// Extract the server response from the answer
preg_match_all('/text\/html\s*(.*)/m', $response, $matches);
$serverResponse = json_decode($matches[1][0]);