php 使用 cURL 接受 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10135269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:28:24  来源:igfitidea点击:

Accept cookies using cURL?

phpcookiescurlfile-get-contents

提问by user1330691

I've been trying to get the contents of a webpage using cURL, but have trouble getting cURL to accept cookies. For example, on Target.com, when I cURL it, it still says that I have to enable cookies.

我一直在尝试使用 cURL 获取网页的内容,但无法让 cURL 接受 cookie。例如,在 Target.com 上,当我对其进行 cURL 时,它仍然说我必须启用 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 权限。

回答by Mike Purcell

077 is an malformed permission setting, this means the owner (probably apache) has no access. Try setting it to 644 (owner has read/write) as it's only a file.

077 是格式错误的权限设置,这意味着所有者(可能是 apache)没有访问权限。尝试将其设置为 644(所有者具有读/写权限),因为它只是一个文件。