2011-08-24 69 views
1

我用下面的代碼登錄到網站PHP捲曲POST數據[重定向302問題]

$postData = array("login" => "Prijava", "loginEmail" => "****@****.***", "password" => "*****t", "signonForwardAction" => "/press/cm/si.press.viasat.tv?cc=si&lc=si"); 

$URL = "http://si.press.viasat.tv/press/cm/1.167?cc=si&lc=si"; 

$connection = curl_init(); 
curl_setopt($connection, CURLOPT_URL, $URL); 
curl_setopt($connection, CURLOPT_POST, 1); 

curl_setopt($connection, CURLOPT_POSTFIELDS, $postData); 

curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true); 

curl_setopt($connection, CURLOPT_REFERER, "http://si.press.viasat.tv"); 
curl_setopt($connection, CURLOPT_AUTOREFERER, true); 
curl_setopt($connection, CURLOPT_HEADER, true); 
curl_setopt($connection, CURLOPT_POSTREDIR, 2); 

curl_setopt($connection,CURLOPT_COOKIEJAR, "C:\@DEV\TextALG\cookie.txt"); 
curl_setopt($connection,CURLOPT_COOKIEFILE, "C:\@DEV\TextALG\cookie.txt"); 

curl_exec($connection); 



curl_close($connection); 

的問題(如在Firebug發現)是登錄後的網站重定向到URL(響應:302 )。結果我再次登錄屏幕。

我得到的cookie這樣的:

# Netscape HTTP Cookie File 
# http://curl.haxx.se/rfc/cookie_spec.html 
# This file was generated by libcurl! Edit at your own risk. 

si.press.viasat.tv FALSE /press FALSE 0 JSESSIONID 00FC3DCA4CFD806CDEBE2CAA7E999463 

任何想法?

+0

它適用於沒有302響應的站點。 –

+0

你試過'curl_setopt($ connection,CURLOPT_HEADER,false);' – diEcho

回答

0

嘗試添加下列選項中

CURLOPT_FOLLOWLOCATION => TRUE,  // follow redirects   
CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 

Reference

2

我嘗試了不同的邏輯(瀏覽邏輯和它的作品吧!)

$ch = curl_init(); 
$randnum = rand(1,5000); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookiejar-$randnum"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookiejar-$randnum"); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 0); 


curl_setopt($ch, CURLOPT_URL,$URL); 
$page = curl_exec($ch); 

preg_match("/action=\"(.*)\"/", $page, $action); 
preg_match("/signonForwardAction\" type=\"hidden\" value=\"(.*)\"/", $page, $signonFA); 

$action = $action[1]; 
$signonFA = $signonFA[1]; 
$postData['signonForwardAction'] = $signonFA; 


curl_setopt($ch, CURLOPT_URL,$URL.$action); 
curl_setopt($ch, CURLOPT_REFERER, $URL); 
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded")); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postData)); 
$page = curl_exec($ch); 

基本想法是讓到現場,設置cookie和發佈數據(必須是字符串不是數組!)到網站(通過$動作獲取),並繼續抓取網站!