【问题标题】:How to stock my cookies from a curl request on a variable如何从变量上的 curl 请求中存储我的 cookie
【发布时间】:2019-05-17 09:52:06
【问题描述】:

我试图阻止 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)。

谢谢

【问题讨论】:

    标签: php curl libcurl


    【解决方案1】:

    我通过对请求结果使用正则表达式来解决我的问题。并使用 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]);
    

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多