2010-05-20 35 views
0

是的,是的,我知道用戶名和密碼。如何從PHP身份驗證背後的網站中大量獲取頁面

我需要一些技巧在PHP中登錄到一個網站和檢索一些圖像/內容,像一個普通的網站。

很明顯,使用捲曲file_get_contents它不起作用,因爲我沒有通過身份驗證。

我該怎麼辦?

驗證是正常的HTTP驗證與POST。

編輯:好的謝謝你的幫助!

我張貼在這裏工作的代碼以供將來參考

//login and set cookie 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_HEADER, 0); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookiefile"); 
curl_setopt($curl, CURLOPT_COOKIEJAR, "cookiefile"); # SAME cookiefile 
curl_setopt($curl, CURLOPT_URL, "url in which there is the login form"); 
curl_setopt($curl, CURLOPT_POSTFIELDS, "user=test&password=test&someparam=somevalue"); //put here the post/get values 
$output = curl_exec($curl); 

echo $output; 

//finally fetch my content 
curl_setopt($curl, CURLOPT_URL, $url_to_fetch); 
$output = curl_exec($curl); 
echo $output; 

curl_close ($curl); 
+0

您是否需要使用PHP以編程方式執行此操作。如果沒有,請嘗試使用wget或類似的東西。網站使用什麼樣的認證?一個HTML表單或HTTP基本認證,或更奇特的東西? – fmark 2010-05-20 15:39:10

回答

1

你可以捲曲認證。 Curl允許發送POST變量進行登錄,並且還支持基本的HTTP身份驗證。

+0

如果我這樣做,我怎麼能保持在會話身份驗證成功和調用curl到其他頁面? – apelliciari 2010-05-20 16:13:18

1

使用瀏覽器來驗證自己的身份,出口餅乾和通過捲曲使用它們。 在會話持續之前,您應該模擬您的用戶。

我在趕時間,不能只是現在爲您提供的代碼,但是我覺得這個方向可以幫助你

可以使用CURLOPT_COOKIEFILE選項來指定在其中存儲的cookie文件。

正如php manual說:

The name of the file containing the cookie data. 
The cookie file can be in Netscape format, or just 
plain HTTP-style headers dumped into a file. 
+0

好提示! thanx – apelliciari 2010-05-21 08:08:19