简体   繁体   English

使用cURL接受cookie?

[英]Accept cookies using cURL?

I've been trying to get the contents of a webpage using cURL, but have trouble getting cURL to accept cookies. 我一直在尝试使用cURL获取网页的内容,但无法让cURL接受cookie。 For example, on Target.com, when I cURL it, it still says that I have to enable cookies. 例如,在Target.com上,当我查看它时,它仍然说我必须启用cookie。

Here is my code: 这是我的代码:

$url = "http://www.target.com/p/Acer-Gateway-15-6-Laptop-PC-NV57H77u-with-320GB-Hard-Drive-4GB-Memory-Black/-/A-13996190#?lnk=sc_qi_detailbutton";
$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 4s
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0"); 
$cookie_file = "cookie1.txt";
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
$result = curl_exec($ch); // run the whole process
curl_close($ch); 
echo $result;

What else am I missing? 我还缺少什么? The cookie1.txt file is 077 permission, by the way. 顺便说一句,cookie1.txt文件是077权限。

077 is an malformed permission setting, this means the owner (probably apache) has no access. 077是格式错误的权限设置,这意味着所有者(可能是apache)无权访问。 Try setting it to 644 (owner has read/write) as it's only a file. 尝试将其设置为644(所有者具有读/写),因为它只是一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM