【问题标题】:curlopt_cookie doesn't seem to be workingcurlopt_cookie 似乎没有工作
【发布时间】:2018-06-18 05:26:51
【问题描述】:

这是我的代码:

$ch = curl_init('http://www1.macys.com/index.ognc');
curl_setopt($ch,CURLOPT_COOKIE,"shippingCountry=US; currency=USD");
curl_setopt ($ch, CURLOPT_COOKIEFILE, "C:/cookie-techenclave.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "C:/cookie-techenclave.txt");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 8);             // Limit redirections to four
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string

$pageString = curl_exec($ch);
curl_close($ch) ;

我尝试设置的 2 个 cookie 未设置。我用netcat对此进行了测试。 这是 netcat 捕获的输出。

D:\php\L.c.MSQL.E.T.DVD\EF\SQL> "D:\My Downloads\apps\nc111nt\nc.exe" -l -p 80
GET /beauty/index_template.php HTTP/1.1
User-Agent: Opera/9.80 (Windows NT 5.1; U; en) Presto/2.10.229 Version/11.64
Host: localhost
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-x
bitmap, */*;q=0.1
Accept-Language: en-US,en;q=0.9
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

可以看出,那里没有 cookie 标头。我究竟做错了什么?我还能尝试什么? 我尝试捕获和设置所有标题,但我尝试设置的 cookie 再次没有任何效果。

谢谢

【问题讨论】:

    标签: php curl


    【解决方案1】:

    如果您希望 curl 根据服务器响应自动更新 cookie 的值,请不要使用 CURLOPT_COOKIE。 因为请求 cookie 将被硬编码为 CURLOPT_COOKIE 的值。在这种情况下,CURLOPT_COOKIEFILE 和 CURLOPT_COOKIEJAR 将被忽略。

    您确定提供的网络嗅探来自上述 PHP 代码吗?我没有在代码中看到 USER_AGENT 设置,但它在您的嗅探中可用。

    【讨论】:

    • 但是如何设置 cookie,没有 curlopt_cookie。是的,用户代理就是浏览器。我删除了 CURLOPT_HTTPHEADER 来测试。
    • CURLOPT_COOKIEFILE & CURLOPT_COOKIEJAR 是你的朋友。要测试一些默认 cookie 值,您应该搜索 curl cookie 文件格式。
    • 成功了!我在 cookie 文件中手动设置了 2 个 cookie。然后设置 curlopt_Cookiefile 和 curlopt_cookiejar ,它终于工作了。谢谢
    • 你能帮我或用正确的答案更新你的问题吗?
    猜你喜欢
    • 2013-11-25
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多