IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Extraire une variable d'une class Curl [PHP 7]


Sujet :

Langage PHP

  1. #1
    Membre éclairé
    Homme Profil pro
    HobbyWeb
    Inscrit en
    Janvier 2005
    Messages
    183
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : Suisse

    Informations professionnelles :
    Activité : HobbyWeb

    Informations forums :
    Inscription : Janvier 2005
    Messages : 183
    Par défaut Extraire une variable d'une class Curl
    Bonjour,

    J'utilise cette class glaner par ici et par ailleurs (cumul de bout de code..), toutefois, cette dernière fait le job . Par contre, je n'arrive pas a récupérer et afficher en dehors de cette class,
    ces 3 variables qui dans la class, affichent bien un résultat

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
                                                            $httpcode = curl_getinfo($this->_url, CURLINFO_HTTP_CODE);
    							$redirectedUrl = curl_getinfo($this->_url, CURLINFO_EFFECTIVE_URL);
    							$CurlErr = curl_error($this->_url); // Définir le bug.....
     
    							echo "HTTP code: $httpcode <br>";
    							echo "Redirection: $redirectedUrl <br>";
    							echo "ErreurCurl: $CurlErr <br>";
    Ce code dans la class

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
     
    	                              class checkstatus 
    					{
    						protected $_url = null;
    						protected $_headers = array();
    						protected $_body = '';
     
    						public function __construct($url,$referer='') 
    						{
    							$this->_url = curl_init($url);
     
    							curl_setopt($this->_url, CURLOPT_URL, $url);
     
    							  $header = Array();
    							  $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
    							  $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
    							  $header[] = "Cache-Control: max-age=0"; 
    							  $header[] = "Connection: keep-alive"; 
    							  $header[] = "Keep-Alive: 300"; 
    							  $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
    							  $header[] = "Accept-Language: fr-FR,fr;q=0.5"; 
    							  $header[] = "Pragma: "; // browsers keep this blank. 
     
     
    								curl_setopt($this->_url, CURLOPT_VERBOSE, true);
    								curl_setopt($this->_url, CURLOPT_RETURNTRANSFER, 1);
    								curl_setopt($this->_url, CURLOPT_AUTOREFERER, 1);
    								curl_setopt($this->_url, CURLOPT_HEADER, 0);
    								curl_setopt($this->_url, CURLOPT_HTTPHEADER, $header);
    								curl_setopt($this->_url, CURLOPT_USERAGENT, 'je teste'); 
    								curl_setopt($this->_url, CURLOPT_FOLLOWLOCATION, 1);
    								curl_setopt($this->_url, CURLOPT_CONNECTTIMEOUT ,5); 
    								curl_setopt($this->_url, CURLOPT_TIMEOUT, 20);
     
    								curl_setopt($this->_url, CURLOPT_MAXREDIRS, 5);
    								curl_setopt($this->_url,CURLOPT_REFERER,$referer); 
    								curl_setopt($this->_url, CURLOPT_HEADERFUNCTION,array($this, 'readHeaders'));
     
    							$output = curl_exec($this->_url);
     
    							$httpcode = curl_getinfo($this->_url, CURLINFO_HTTP_CODE);
    							$redirectedUrl = curl_getinfo($this->_url, CURLINFO_EFFECTIVE_URL);
    							$CurlErr = curl_error($this->_url); // Définir le bug.....
     
    							echo "HTTP code: $httpcode <br>";
    							echo "Redirection: $redirectedUrl <br>";
    							echo "ErreurCurl: $CurlErr <br>";			
     
    							//curl_close($ch);
     
    							}
                                                                   public function __destruct() 
    								{
    									curl_close($this->_url);
    								}
     
    								public function getHeaders() 
    								{
    									$this->_body = curl_exec($this->_url);
    									return $this->_headers;
    								}
     
    								public function getBody() 
    								{
    									return $this->_body;
    								}
     
    								public function getInfo($info_type) 
    								{
    									$info = curl_getinfo($this->_url );
    									return $info[$info_type];
    								}
     
    								protected function readHeaders($url, $str) 
    								{
    									if (mb_strlen($str) > 0) 
    									{
    										$this->_headers[] = $str;
    									}
    									return mb_strlen($str);
    								}
     
    						}
    Dès lors, comment récupérer ces 3 variables ailleurs dans ma page ? Ainsi , je pourrai travailler avec des conditions selon le résultat récupéré

    D'avance merci pour votre aide/exemple.

    Yule

  2. #2
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 506
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 506
    Par défaut
    Si vous désirez utiliser des attributs en dehors de vos méthodes, il suffit des les créer et les initialiser:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
     
    ....
     
        protected $_url = null;
        protected $_headers = array();
        protected $_body = '';
     
        // Nouveaux attributs
        public $httpcode = null;
        public $redirectedUrl = null;
        public $CurlErr = null;
     
     
     
     
        public function __construct($url, $referer = '')
        {
            $this->_url = curl_init($url);
     
            curl_setopt($this->_url, CURLOPT_URL, $url);
     
            $header = array();
            $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
            $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
            $header[] = "Cache-Control: max-age=0";
            $header[] = "Connection: keep-alive";
            $header[] = "Keep-Alive: 300";
            $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            $header[] = "Accept-Language: fr-FR,fr;q=0.5";
            $header[] = "Pragma: "; // browsers keep this blank.
     
     
            curl_setopt($this->_url, CURLOPT_VERBOSE, true);
            curl_setopt($this->_url, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($this->_url, CURLOPT_AUTOREFERER, 1);
            curl_setopt($this->_url, CURLOPT_HEADER, 0);
            curl_setopt($this->_url, CURLOPT_HTTPHEADER, $header);
            curl_setopt($this->_url, CURLOPT_USERAGENT, 'je teste');
            curl_setopt($this->_url, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($this->_url, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($this->_url, CURLOPT_TIMEOUT, 20);
     
            curl_setopt($this->_url, CURLOPT_MAXREDIRS, 5);
            curl_setopt($this->_url, CURLOPT_REFERER, $referer);
            curl_setopt($this->_url, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders'));
     
            $output = curl_exec($this->_url);
     
            $this->httpcode = curl_getinfo($this->_url, CURLINFO_HTTP_CODE);
            $this->redirectedUrl = curl_getinfo($this->_url, CURLINFO_EFFECTIVE_URL);
            $this->CurlErr = curl_error($this->_url); // Définir le bug.....
     
            echo "HTTP code: $this->httpcode <br>";
            echo "Redirection: $this->redirectedUrl <br>";
            echo "ErreurCurl: $this->CurlErr <br>";
     
            //curl_close($ch);
     
        }
    .....
    Du coup, comme vous les instancier dans votre construct, vous aurez accès à ces attributs via $this->...

  3. #3
    Membre éclairé
    Homme Profil pro
    HobbyWeb
    Inscrit en
    Janvier 2005
    Messages
    183
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : Suisse

    Informations professionnelles :
    Activité : HobbyWeb

    Informations forums :
    Inscription : Janvier 2005
    Messages : 183
    Par défaut
    Parfait et merci bien pour le temps passé et la solution
    Bonne soirée
    Yule

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Extraire une classe d'une librarie packagé en JAR
    Par MagnetiK dans le forum Général Java
    Réponses: 5
    Dernier message: 06/12/2011, 09h36
  2. extraire une classe d'une DLL
    Par bbenator dans le forum C++
    Réponses: 4
    Dernier message: 15/09/2010, 15h34
  3. [FLASH MX2004] [AS2] Accès aux variables d'une classe
    Par stailer dans le forum ActionScript 1 & ActionScript 2
    Réponses: 2
    Dernier message: 17/07/2005, 13h58
  4. Variables globales dans une classe
    Par vanitom dans le forum C++
    Réponses: 16
    Dernier message: 14/01/2005, 15h40
  5. Variable d'une Classe Ancêtre
    Par Génie dans le forum Langage
    Réponses: 3
    Dernier message: 18/09/2002, 20h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo