curl php正确设置cookie [英] curl php setting cookies properly

查看:252
本文介绍了curl php正确设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些cookie烦恼。他们似乎没有设置,因为他们应该,我没有能够弄清楚为什么。网址加载网址详细信息,然后设置Cookie,然后将其重定向回主网站。这里是我使用的PHP代码

I'm having some cookie troubles. They don't seem to be setting as they should be, and I haven't been able to figure out why. The url loads the urls details and then sets the cookie and then redirects them back to the main site. Here's the php code I am using

$cookie_file_path = 'cookie-path.txt';

// open a site with cookies
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.stuller.com/s/user');
curl_setopt($ch, CURLOPT_HEADER  ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);   // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
$content = curl_exec($ch);

// get cookies
$cookies = array();
preg_match_all('/Set-Cookie:(?<cookie>\s{0,}.*)$/im', $content, $cookies);

echo "<pre>";
print_r($cookies['cookie']); // show harvested cookies
echo "</pre>";
exit;

当我回声时,这是我看到的:

When I echo out the cookies this is what I see:

Array(
[0] =>  cart_session_id=365ad5b7-9ece-4255-aa68-3590d8841c17; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[1] =>  CartRefresh=5/19/2014 2:05:46 PM; expires=Tue, 20-May-2014 07:05:46 GMT; path=/
[2] =>  ASP.NET_SessionId=yyloxijj2mke1ozxwg203mxh; path=/; HttpOnly
[3] =>  user_session_id=27c284ec-c5da-4c79-b860-f7bce9efac78; path=/
[4] =>  cms#51642=12719; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[5] =>  cms#51772=152; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[6] =>  cms#52083=13824; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[7] =>  cms#52113=32; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[8] =>  cms#52229=50; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
[9] =>  cms#52237=59; expires=Tue, 19-May-2015 19:05:46 GMT; path=/
}

但在我的cookies文件是假的,没有设置:

But in my cookies file it seems like everything is false and nothing is set:

www.stuller.com FALSE   /   FALSE   1432062346  cart_session_id 365ad5b7-9ece-4255-aa68-3590d8841c17
www.stuller.com FALSE   /   FALSE   1400569546  CartRefresh 5/19/2014 2:05:46 PM
#HttpOnly_www.stuller.com   FALSE   /   FALSE   0   ASP.NET_SessionId   yyloxijj2mke1ozxwg203mxh
www.stuller.com FALSE   /   FALSE   0   user_session_id 27c284ec-c5da-4c79-b860-f7bce9efac78
www.stuller.com FALSE   /   FALSE   1432062346  cms#51642   12719
www.stuller.com FALSE   /   FALSE   1432062346  cms#51772   152
www.stuller.com FALSE   /   FALSE   1432062346  cms#52083   13824
www.stuller.com FALSE   /   FALSE   1432062346  cms#52113   32
www.stuller.com FALSE   /   FALSE   1432062346  cms#52229   50
www.stuller.com FALSE   /   FALSE   1432062346  cms#52237   59

做错了,为什么他们不正确设置?

What am I doing wrong and why aren't they setting properly? And also, how can I read those cookies for the next curl method(s) as there will be about 4 or 5 hops that happen?

感谢

推荐答案

当您要发送 Cookie时,请使用 CURLOPT_COOKIEFILE

When you want to send cookies, use the CURLOPT_COOKIEFILE option.

当您想要接收 cookies并储存时,请使用 CURLOPT_COOKIEJAR

When you want to receive cookies and save them, use the CURLOPT_COOKIEJAR option.

如果您要发送以前保存的Cookie,并使用服务器进行的任何更改更新文件,请使用这两个选项。

If you want to send previously-saved cookies and also update the file with any changes the server makes, use both options.

这篇关于curl php正确设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆