php 在php中使用Curl时读取Cookie,如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9714360/
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-26 07:29:06  来源:igfitidea点击:

Reading Cookie when using Curl in php, how to?

phpcurl

提问by Diode

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

我正在连接到使用 cookie 对用户进行身份验证的 API 服务。我从命令提示符发出这两个语句并且它有效。

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.phpand get_data.phpusing curl.

现在我想使用 curl制作两个等效的 php 文件login.phpget_data.php

I am using

我在用

curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);

in login.php

login.php

and

curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);

in get_data.php

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.

它不工作。正在创建 Cookie 文件,但第二个 curl 没有读取它。这是正确的方法吗?我是否必须单独读取 cookie 文件并设置标题Cookie?任何帮助将不胜感激。谢谢。

回答by Joseph Lust

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

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

<?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 examplesfor how to effectively parse such as string.

有关如何有效解析字符串的其他示例,请参见其他示例

回答by tameren

is there any method to see the curl request headers. Can I get it from curl instance ? or is there any tool like fiddler for Mac (I am working Mac OS ) ?

有什么方法可以查看 curl 请求标头。我可以从 curl 实例中获取它吗?或者有没有像 fiddler for Mac 这样的工具(我正在使用 Mac OS)?

Yes, you can get the request headers with the following:

是的,您可以使用以下内容获取请求标头:

<?php
  ...
  curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking
  $result = curl_exec($ch);
  var_dump(curl_getinfo($ch, CURLINFO_HEADER_OUT)); // request headers
?>

回答by Adam

In PHP 5.5.0 it looks like you can now get the cookies in one line but I only get back an empty array using this with PHP 7.1.0:

在 PHP 5.5.0 中,看起来您现在可以在一行中获取 cookie,但我只能在 PHP 7.1.0 中使用它返回一个空数组:

CURLINFO_COOKIELIST - get all known cookies

CURLINFO_COOKIELIST - 获取所有已知的 cookie

$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);