【问题标题】:How Can I Make Curl Get The Cookies [duplicate]我怎样才能让卷曲得到饼干[重复]
【发布时间】:2013-01-08 21:08:20
【问题描述】:

可能重复:
how to get the cookies from a php curl into a variable

我正在运行下面的代码,它的作用是使用 curl 获取网页。我的问题是当它获取网页时,它没有从网站获取 cookie。

   $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, ''.$stuff_link[0].'');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    $html = curl_exec($ch);
    curl_close ( $ch );
    echo $html;

我尝试了一些方法,但都没有奏效。

【问题讨论】:

  • 我看到那个帖子,厌倦了它不起作用
  • 请将代码更新为您尝试/使用的代码

标签: php


【解决方案1】:
$ch = curl_init('http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
// get cookie, all cos sometime set-cookie could be more then one
preg_match_all('/^Set-Cookie:\s*([^\r\n]*)/mi', $result, $ms);
// print_r($result);
$cookies = array();
foreach ($ms[1] as $m) {
    list($name, $value) = explode('=', $m, 2);
    $cookies[$name] = $value;
}
print_r($cookies);

【讨论】:

  • 感谢它打印 cookie 但它们没有在我的浏览器中设置
  • 是的,没错。服务器 (URL) 试图设置 cookie 但不能,因为 cookie 转到 CURL(简单来说)。所以它与这个(命令行)相同:curl -v http://google.com/。但是,如果您使用浏览器打开 URL,这些 cookie 将设置在您的浏览器上。
【解决方案2】:

试试这个:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "HTTP://URLHERE.COM");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
$html = curl_exec($ch);
preg_match('/^Set-Cookie: (.*?);/m', curl_exec($ch), $m);
var_dump(parse_url($m[1]));
curl_close ( $ch );
echo $html;

重复的问题几乎来自: how to get the cookies from a php curl into a variable

【讨论】:

  • 谢谢我看到那个帖子我试过了但它没有工作它仍然没有得到所有的cookie并为我设置它们。
  • 请向我们提供您正在使用的代码示例及其返回的内容
  • 我正在使用上面的代码,并且 $stuff_link[0] 被设置为一个作为 cookie 的链接,但是当 curl 运行时它不会加载 cookie
  • 它使用上面的代码返回这个 array(1) { ["path"]=> string(0) "" }
猜你喜欢
  • 2015-06-06
  • 2010-12-24
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 2010-12-24
相关资源
最近更新 更多