php 如何在 CURL 重定向上传递 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1458683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:40:17  来源:igfitidea点击:

How do I pass cookies on a CURL redirect?

phpcookiesredirectcurl

提问by acme

imagine the following scenario: I open a CURL connection and pass some XML-Logindata via POST. The server answers with an 302 redirect, where the session cookies are set and redirects me to a following "welcome"-page. If I enable FOLLOWLOCATION the cookies set on the redirection-page get lost and the welcome-page fails with a "session expired"-message. If I disable FOLLOWLOCATION, I'm not redirected (obviously) and get a HTML-page with "the page has moved to another location" with a link that leads me to the welcome-page. This works as the cookies are set, but I need to follow the redirect and go straight to the welcome-page.

想象以下场景:我打开一个 CURL 连接并通过 POST 传递一些 XML-Logindata。服务器以 302 重定向进行响应,其中设置了会话 cookie 并将我重定向到以下“欢迎”页面。如果我启用 FOLLOWLOCATION,重定向页面上设置的 cookie 会丢失,欢迎页面失败并显示“会话已过期”消息。如果我禁用 FOLLOWLOCATION,我不会被重定向(显然)并获得一个带有“页面已移动到另一个位置”的 HTML 页面,其中包含一个将我引导到欢迎页面的链接。这在设置 cookie 时起作用,但我需要遵循重定向并直接转到欢迎页面。

So, how do I maintain the cookies so that they are set correctly?

那么,我如何维护 cookie 以便正确设置它们?

This is my code so far:

到目前为止,这是我的代码:

$ch = curl_init('https://www.example.com/login');

curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, '<some xml data>');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=UTF-8"));

curl_exec($ch);
curl_close($ch)

Thanks for any help! ;

谢谢你的帮助!;

回答by astonia

This is an old question, but I had the same problem, so google took me here. Finally, I managed to solve it. By passing an empty string "" to set CURLOPT_COOKIEFILE using curl_setopt will solve the problem:

这是一个老问题,但我遇到了同样的问题,所以谷歌把我带到了这里。最后,我设法解决了它。通过使用 curl_setopt 传递一个空字符串 "" 来设置 CURLOPT_COOKIEFILE 将解决问题:

curl_setopt($ch, CURLOPT_COOKIEFILE, "");

See section CURLOPT_COOKIEFILEof http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

CURLOPT_COOKIEFILEhttp://curl.haxx.se/libcurl/c/curl_easy_setopt.html

回答by Gabriel Braila

to instruct php on curl session to use cookies you should set two options:

要在 curl 会话中指示 php 使用 cookie,您应该设置两个选项:

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');// set where cookies will be stored
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');// from where it will get cookies

so every cookie will be appended on CURLOPT_COOKIEJAR and those cookie will be carried on every location by setting CURLOPT_COOKIEFILE

所以每个 cookie 都会被附加到 CURLOPT_COOKIEJAR 上,并且这些 cookie 将通过设置 CURLOPT_COOKIEFILE 被携带到每个位置

回答by acme

To answer myself, this is how I did it:

为了回答自己,我是这样做的:

Grab the header-http-status code. If it's redirect then extract the new location and redirect manually. Otherwise remove the header and output the contents:

获取 header-http-status 代码。如果是重定向,则提取新位置并手动重定向。否则删除标题并输出内容:

$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

if($info['http_code'] == 301 || $info['http_code'] == 302) { // redirect manually, cookies must be set, which curl does not itself

    // extract new location
    preg_match_all('|Location: (.*)\n|U', $response, $results);
    $location = implode(';', $results[1]);

    // redirect manually
    header("Location: $location");
    exit;

} else { // no redirect, remove header and output directly

    $response = substr_replace($response, '', 0, strpos($response, '<', 0));

    echo $response;

}

回答by Sean Huber

You might want to check out this library too: http://github.com/shuber/curl

你可能也想看看这个库:http: //github.com/shuber/curl