1

重複の可能性:
php curl から変数に Cookie を取得する方法

以下のコードを実行しており、curl を使用して Web ページを取得しています。私の問題は、Web ページを取得したときに、サイトから 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;

かなり重複した質問: how to get the cookies from a php curl into a variable

于 2013-01-24T23:28:40.637 に答える