php - 使用 cURL 接受 cookie?

标签 php cookies curl file-get-contents

我一直在尝试使用 cURL 获取网页内容,但无法让 cURL 接受 cookie。 例如,在 Target.com 上,当我对其进行 cURL 时,它仍然提示我必须启用 cookie。

这是我的代码:

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

我还缺少什么? 顺便说一下,cookie1.txt 文件是 077 权限。

最佳答案

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

关于php - 使用 cURL 接受 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135269/

相关文章:

php - Symfony2 FOSUserBundle 无法更新 Doctrine 数据库模式或清除缓存

php - 在导航中使用语言按钮切换语言 Wordpress

Android 异步 Http 客户端 (loopj) 和持久性 Cookie 存储

javascript - 不同页面上的相同 cookie(cookie 路径)

php - 如何在easyphp 16.1.1中使用PHP PDO连接MySQL

php - 统计月初以来的优惠券总值(value)

wordpress - WordPress和CakePHP之间的单点登录(SSO)

php - 使用 CURL 发布方法的 Google 翻译 API

ssl - 从 curl 获取 (58) 无法使用客户端证书(未找到 key 或密码短语错误?)

php - Postman:如何在Postman中执行Paypal CURL请求?