PHP cURL cookie无法维​​护

时间:2017-10-22 06:02:48

标签: php curl cookies

我想多次cURL一个地址并在它们之间共享cookie文件。我使用的代码如下:

<?php

$count = $argv[1];

echo $count;

while($count > 0){

$ch = curl_init("somewhere.php");

curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookeis.txt");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );

$count--;

echo $count;

// grab URL and pass it to the browser
echo curl_exec($ch) . "\n\n";

// close cURL resource, and free up system resources
curl_close($ch);
}

?>

但在运行代码后,我无法通过网络浏览器多次查看请求网页的结果。尽管如此填写了cookies文件。

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

somewhere  FALSE   /       FALSE   0       PHPSESSID       something

哪里是错误的? TG

1 个答案:

答案 0 :(得分:0)

我发现了错误。应该添加以下代码行:

...
$cookie = session_name() . '=' . time();
...
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
...

TG