2012-11-05 51 views
0

我試圖做的是登錄到一個網站,然後去抓取表中的數據,因爲它們沒有導出功能。到目前爲止,我已設法登錄,並向我顯示用戶主頁。不過,我需要導航到不同的頁面或以某種方式抓取該頁面,同時仍然使用curl登錄。登錄後用cURL從網站抓取數據?

到目前爲止我的代碼:

$username="email"; 
$password="password"; 
$url="https://jiltapp.com/sessions"; 
$cookie="cookie.txt"; 
$url2 = "https://jiltapp.com/shops/shopname/orders"; 

$postdata = "email=".$username."&password=".$password; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result; 
curl_close($ch); 

正如我所說,我得到訪問主要用戶頁面,但我需要搶$ URL2變量,而不是$ URL的內容。我怎麼能做到這樣的事情?

謝謝!

+0

你只做一個捲曲。你怎麼可能期望從第二頁獲取信息? – thatidiotguy

+1

我並不期待它,我不知道該怎麼做大聲笑 – user1701398

回答

6

登錄後,再次請求包含您之後數據的頁面。

對於後續的請求,您必須設置指向與CURLOPT_COOKIEJAR相同的文件的選項CURLOPT_COOKIEFILE。 cURL將從該文件中讀取cookie並將其發送給請求。

$username="email"; 
$password="password"; 
$url="https://jiltapp.com/sessions"; 
$cookie="cookie.txt"; 
$url2 = "https://jiltapp.com/shops/shopname/orders"; 

$postdata = "email=".$username."&password=".$password; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // <-- add this line 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result; 

// make second request 

$url = 'page you want to get data from'; 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, 0); 

$data = curl_exec($ch); 
+0

你太棒了!謝謝。 – user1701398

+0

@drew我有類似的問題請幫助: http://stackoverflow.com/questions/29875871/php-curl-post-data-to-get-value-form-referred-url – mydeve