在php中使用Curl时阅读Cookie,如何? [英] Reading Cookie when using Curl in php, how to?

查看:47
本文介绍了在php中使用Curl时阅读Cookie,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接到API服务,该服务使用Cookie对用户进行身份验证.我在命令提示符下做出了这两个语句,并且可以正常工作.

  curl -d"u = username& p = password" -c〜/cookiejar https://domain/login卷曲-b https://domain/getData 

现在,我要使用curl制作两个等效的php文件 login.php get_data.php .

我正在使用

  curl_setopt($ ch,CURLOPT_COOKIEJAR,$ ckfile); 

login.php

  curl_setopt($ ch,CURLOPT_COOKIEFILE,$ ckfile); 

get_data.php

它不起作用.Cookie文件已创建,但第二个curl无法读取它.这是正确的方法吗?我是否需要分别读取cookie文件并设置标题 Cookie ?任何帮助,将不胜感激.谢谢.

解决方案

这可以解决问题.我以Google.com为例运行它:

 <?PHP//打开包含Cookie的网站$ ch = curl_init();curl_setopt($ ch,CURLOPT_URL,"http://www.google.com");curl_setopt($ ch,CURLOPT_USERAGENT,'Mozilla/5.0(Windows NT 6.1; rv:11.0)Gecko/20100101 Firefox/11.0');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);$ content = curl_exec($ ch);//获取Cookie$ cookies = array();preg_match_all('/Set-Cookie:(?< cookie> \ s {0,}.*)$/im',$ content,$ cookies);print_r($ cookies ['cookie']);//显示收获的Cookie//Cookie字符串的基本解析(仅作为示例)$ cookieParts = array();preg_match_all('/Set-Cookie:\ s {0,}(?P< name> [^ =] *)=(?P< value> [^;] *).*?expires =(?P< expires>[^;] *).*?path =(?P  [^;] *).*?domain =(?P  [^ \ s;] *).*?$/im',$ content,$ cookieParts);print_r($ cookieParts);?> 

有关如何有效解析(例如字符串)的信息,请参见其他示例./p>

I am connecting to an API service which authenticates users using cookies. I make these two statements from command prompt and it works.

curl -d "u=username&p=password" -c ~/cookiejar https://domain/login

curl -b https://domain/getData

Now I want to make two equivalent php files login.php and get_data.php using curl.

I am using

curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);

in login.php

and

curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);

in get_data.php

It is not working. Cookie file is getting created but second curl is not reading it. Is this the right way to do it ? Do I have to read the cookie file seperately and set the header Cookie ? Any help would appreciated. Thanks.

解决方案

This will do the trick. I run it against Google.com as an example:

<?PHP

// open a site with cookies
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.google.com");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0');
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);
$content = curl_exec($ch);

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

print_r($cookies['cookie']); // show harvested cookies

// basic parsing of cookie strings (just an example)
$cookieParts = array();
preg_match_all('/Set-Cookie:\s{0,}(?P<name>[^=]*)=(?P<value>[^;]*).*?expires=(?P<expires>[^;]*).*?path=(?P<path>[^;]*).*?domain=(?P<domain>[^\s;]*).*?$/im', $content, $cookieParts);
print_r($cookieParts);

?>

See other examples for how to effectively parse such as string.

这篇关于在php中使用Curl时阅读Cookie,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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