J'ai suivi le tutoriel de Jilp sur l'utilisation de la librairie Curl http://julp.developpez.com/php/curl/#L3.1.4 et je doit avouer que je n'arrive pas à m'identifier sur un site.
J'utilise wamp, Curl est activé, et j'utilise PHP 5.3
Voici le code que j'utilise:
<?php
define('LOGIN', 'xxx);
define('PASSWORD', 'xxx');
define('AUTHENTIFICATION', 'http://forum.wawa-mania.ws/login.php');
define('PAGE_PRIVEE', 'http://forum.wawa-mania.ws/viewtopic.php?id=522363');
$sid = '';
$ch = curl_init(AUTHENTIFICATION);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array(
'username' => LOGIN,
'password' => PASSWORD
)
);
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath('cookie.txt'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$ret = curl_exec($ch);
if ($ret === FALSE) {
die(curl_error());
}
curl_close($ch);
if (preg_match('/(PHPSESSID=[0-9a-z,-]{32,40})/i', $ret, $m)) {
$sid = '?' . $m[1];
} else if (preg_match('#<input\s+type="hidden"\s+name="([^\r\n\t <>\'"\\\]+)"\s+value="([0-9a-z,-]{32,40})"\s*/?>#i', $ret, $m)) {
$sid = '?' . $m[1] . '=' . $m[2];
}
$ch = curl_init(PAGE_PRIVEE . $sid);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
$ret = curl_exec($ch);
if ($ret === FALSE) {
die(curl_error());
}
curl_close($ch);
echo $ret;
?>
Les champs 'username' et 'password' du site sont correct.
J'ai une page blache qui s'affiche et lorsque je regarde si le fichier cookie.txt est remplie, il est vide.
Grâce au petit plug-in de firefow qui sert a voir les entêtes HTTP, j'ai constaté que le script arrive à se connecter, mais aucun cookie.
- http://localhost/wawamania%20auto/conne ... ession.php
GET /wawamania%20auto/connect_with_session.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cache-Control: max-age=0
HTTP/1.1 200 OK
Date: Thu, 05 Aug 2010 16:02:57 GMT
Server: Apache/2.2.11 (Win32) PHP/5.3.0
X-Powered-By: PHP/5.3.0
Content-Length: 4
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
----------------------------------------------------------