1

可能重复:
如何将 php curl 中的 cookie 获取到变量中

我正在运行下面的代码,它的作用是使用 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;

我尝试了一些没有奏效的东西。

4

2 回答 2

6
$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);
于 2013-01-25T01:22:17.500 回答
0

尝试这个:

$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;

重复的问题几乎来自: 如何从 php curl 中获取 cookie 到变量中

于 2013-01-24T23:28:40.637 回答