2011-08-24 7 views
1

J'utilise le code suivant pour vous connecter à un site WebPHP cURL POST DATA [redirect 302 problème]

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

Le problème est (que l'on trouve dans Firebug) que vous connecter après le site redirect à l'URL (Réponse: 302). Et que par résultat, je reçois à nouveau l'écran de connexion.

Je reçois biscuit comme ceci:

# 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 

Toutes les idées?

+0

Cela fonctionne très bien pour les sites sans réponse 302. –

+0

avez-vous essayé avec 'curl_setopt ($ connection, CURLOPT_HEADER, false);' – diEcho

Répondre

0

essayez d'ajouter ci-dessous les options

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

Reference

2

j'ai essayé une logique différente (logique de navigation et il fonctionne maintenant!)

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

Basic idée est d'obtenir sur le site, mettre des cookies et que publier des données (doit être chaîne non array!) sur le site (get by $ action) et que de continuer à explorer le site!