IMPOSSIBLE de se connecter à un site via cURL en POST

Résolu
diego_9708 Messages postés 4 Statut Membre -  
jordane45 Messages postés 30427 Date d'inscription   Statut Modérateur Dernière intervention   -
Salut, j'essaye de me connecté à un site avec cURL en POST

Avent de faire le script j'ai chercher dans les requettes http les données qui était envoyer lors d'une connexion réussite



J'ai regarder aussi dans le formulaires html
Sauf je n'arrive pas à connecter

Enfaite je chercher à me connecter, puis après maintenir la connexion et essayer de récupérer le contenue d'une page en étant connecter

Je vous met le code PHP

<?php


/* Connexion au site */
$URL= 'https://www.x/welcome.php';

$POST_DATA = 
[

    'username' => 'Project1',
    'password' => 'azertyuiop123',
    'x' => 0,
    'y' => 0
];

$COOKIES = 'cookies.txt';


$ch = curl_init($URL);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST_DATA);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath($COOKIES));

curl_exec($ch);
curl_close($ch);

/* Récupération de la page */

$URL2 = 'https://www.x/messages.php';
$ch2 = curl_init($URL2);



curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $POST_DATA);
curl_setopt($ch2, CURLOPT_COOKIEFILE, realpath($COOKIES));

$contenu_de_la_page = curl_exec($ch2);

curl_close($ch2);

echo $contenu_de_la_page;
unlink($COOKIES);


Alors j'ai essayer plusieurs chose avant de poster ici, j'ai essayer de remplacer $ch2 par $ch pour la récupération de la page, mais sauf lorsque je lance le script il n'affiche le formulaire en html, or il devrez m'afficher la page message.php

J'ai vérifier aussi que le fichier cookies.txt avait bien les droits de lectures et écritures et ainsi pour le script, j'ai vérifier que le fichier cookies.txt se remplisez bien

Je ne voit pas dutout d'ou peut provenir le problèmes, welcome.php est bien renseignez dans le formulaire html et dans les http header, et mon username et password et bien correcte,

Merci pour ceux qui prendrons le temps de me répondre merci d'avence, aurevoir

1 réponse

  1. jordane45 Messages postés 30427 Date d'inscription   Statut Modérateur Dernière intervention   4 831
     
    Bonjour,

    Et avec un truc du genre :
    <?php
    /* Connexion au site */
    
    
    class ccurl{
     private $ch = null;
     private $url_connect = 'https://www.x/welcome.php';
     private $url_datas = 'https://www.x/messages.php';
     private $username = 'Project1';
     private $password = 'azertyuiop123';
     private $cookies_file = __DIR__.'\cookies.txt';
     
     function __construct(){
       $this->ch = curl_init();
       $this->connect();
       
     }
     
     private function connect(){
        $timeout = 1000;
       //Initialise une nouvelle connexion
        curl_setopt($this->ch, CURLOPT_URL, $this->url_connect);
        curl_setopt($this->ch, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); 
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($this->ch, CURLOPT_NOBODY, true);
     
        //Initialise les cookies
        curl_setopt($this->ch, CURLOPT_COOKIESESSION, true);
     
        // Saisie des paramètres de connexion
        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, array(
            'username' => $this->username,
            'password' => $this->password,
            'x' => 0,
            'y' => 0
        ));
        // Sauvgarde de la session dans un cookie
        curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookies_file);
        // Lecture du cookie enregistré
        curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookies_file);
        $res = curl_exec($this->ch);
        if($res==false){
          echo 'Erreur lors de la connexion : ' . curl_error($ch);
          exit();
        }
      }
      
      public function getDatas(){
        curl_setopt($this->ch, CURLOPT_URL, $this->url_datas);
        curl_setopt($this->ch, CURLOPT_POST, false);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, "");
        $res = curl_exec($this->ch);
     
        if($res==false){
          echo 'Erreur lors de la connexion : ' . curl_error($ch);
          exit();
        }else{
          return $res;
        }
      }
      
      public function close(){
        curl_close($this->ch);
      }
    }
    
    //et pour l'utiliser :
    $oCurl = new ccurl();
    $datas = $oCurl->getDatas();
    $oCurl->close();
    print_r($datas);
     
    ?>
    


    ça donne quoi ?

    1
    1. diego_9708 Messages postés 4 Statut Membre
       
      Wow ! super sa merci ! sinon c'est toi qui à fait le code? j'aimerais savoir d'ou venez mon problèmes
      0
      1. jordane45 Messages postés 30427 Date d'inscription   Statut Modérateur Dernière intervention   4 831 > diego_9708 Messages postés 4 Statut Membre
         
        Il faut garder la même variable ch.
        0