2012-04-13 60 views
3

我一直在嘗試使用cURL獲取網頁的內容,但無法讓cURL接受cookie。 例如,在Target.com上,當我捲曲它時,它仍然說我必須啓用cookie。使用cURL接受cookies?

這裏是我的代碼:

$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權限。

+4

077是無效的權限,這意味着所有者(可能是apache)無權訪問。嘗試將它設置爲644,因爲它只是一個文件。 – 2012-04-13 04:46:06

+0

@MikePurcell你爲什麼要發表評論?發佈它作爲答案。 – 2012-04-13 11:25:42

+1

@Truth:不確定它是不是一個類型。 – 2012-04-13 14:36:38

回答

4

077是一個格式錯誤的權限設置,這意味着所有者(可能是apache)無權訪問。嘗試將其設置爲644(所有者已讀/寫),因爲它只是一個文件。

+2

嗨,你是否知道你的問題? – 2012-04-18 00:29:48