Curl - Formulaire

ldenis
Invité n'ayant pas de compte PHPfrance

11 mai 2012, 21:12

Bonjour,
je fait mes débuts sur cURL, mais j'Ai deja un petit probleme, je souhaite envoyer un formulaire automatiquement, mais rien ne ce passe, je ne vois pas trop d'Ou cela peu venir si quelqu'un a une idée
<?php
 
 
$fp = fopen("cookies.txt",'wb');	
fclose($fp);
 
 
$postfields = array();
$postfields["action"] = "submit";
$postfields["nom"] = "test";
$postfields["email"] = "[email protected]";
$postfields["message"] = "test";
 
 


$url = "http://denis-leo.com/contact.php";
$useragent = "Mozilla/5.0";
$referer = $url; 
 
 

$ch = curl_init($url);
 curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
 

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);


curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

curl_setopt($ch, CURLOPT_REFERER, $referer);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


$result = curl_exec($ch);
curl_close($ch);
 

echo $result;
?>

ViPHP
ViPHP | 1380 Messages

14 mai 2012, 13:52

Une page blanche en cURL est souvent le signe d'une redirection non suivie. Ton formulaire ressemble à un login. Il y a donc de fortes chances qu'il y en ait une. Rajoute l'option suivante:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
Pour debuger cURL, il est intéressant d'ajouter l'affichage du header de réponse du serveur. S'il y a une redirection, tu devrais y trouver un code dans les 3xx. Pour afficher la réponse du serveur:
curl_setopt($ch, CURLOPT_HEADER, 1);
ripat