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

PHP & Base de données Discussion :

Reponse api rest et guzzle


Sujet :

PHP & Base de données

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Octobre 2012
    Messages
    69
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2012
    Messages : 69
    Par défaut Reponse api rest et guzzle
    Bonjour,

    Je rencontre un problème avec le retour d'une api rest couplée avec Guzzle.

    code coté service avec un message d'erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    echo json_encode(array("alert" => "User not found"));
    code coté guzzle :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    try{
    $response = $this->client->request('POST', 'url', ...);
    }catch(\GuzzleHttp\Exception\ClientException $e){
    var_dump($e->getResponse()->getBody()->alert);
    }
    le var_dump retourne null alors que je devrais afficher "User not found"

  2. #2
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 670
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 670
    Par défaut
    et que donne l'affichage de la réponse entière ?

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Octobre 2012
    Messages
    69
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2012
    Messages : 69
    Par défaut
    object(GuzzleHttp\Psr7\Response)[33]
    private 'reasonPhrase' => string 'Not Found' (length=9)
    private 'statusCode' => int 404
    private 'headers' =>
    array (size=14)
    'Date' =>
    array (size=1)
    0 => string 'Fri, 13 Dec 2019 12:45:30 GMT' (length=29)
    'Server' =>
    array (size=1)
    0 => string 'Apache' (length=6)
    'Expires' =>
    array (size=1)
    0 => string 'Thu, 19 Nov 1981 08:52:00 GMT' (length=29)
    'Cache-Control' =>
    array (size=1)
    0 => string 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0' (length=62)
    'Pragma' =>
    array (size=1)
    0 => string 'no-cache' (length=8)
    'Access-Control-Allow-Origin' =>
    array (size=1)
    0 => string '*' (length=1)
    'Access-Control-Allow-Headers' =>
    array (size=1)
    0 => string 'access' (length=6)
    'Access-Control-Allow-Methods' =>
    array (size=1)
    0 => string 'GET' (length=3)
    'Access-Control-Allow-Credentials' =>
    array (size=1)
    0 => string 'true' (length=4)
    'Set-Cookie' =>
    array (size=1)
    0 => string 'PHPSESSID=q3lcvgp8qme9dsasaepclgicc5; path=/' (length=44)
    'Strict-Transport-Security' =>
    array (size=1)
    0 => string 'max-age=31536000' (length=16)
    'Content-Security-Policy' =>
    array (size=1)
    0 => string 'upgrade-insecure-requests' (length=25)
    'Transfer-Encoding' =>
    array (size=1)
    0 => string 'chunked' (length=7)
    'Content-Type' =>
    array (size=1)
    0 => string 'application/json' (length=16)
    private 'headerNames' =>
    array (size=14)
    'date' => string 'Date' (length=4)
    'server' => string 'Server' (length=6)
    'expires' => string 'Expires' (length=7)
    'cache-control' => string 'Cache-Control' (length=13)
    'pragma' => string 'Pragma' (length=6)
    'access-control-allow-origin' => string 'Access-Control-Allow-Origin' (length=27)
    'access-control-allow-headers' => string 'Access-Control-Allow-Headers' (length=28)
    'access-control-allow-methods' => string 'Access-Control-Allow-Methods' (length=28)
    'access-control-allow-credentials' => string 'Access-Control-Allow-Credentials' (length=32)
    'set-cookie' => string 'Set-Cookie' (length=10)
    'strict-transport-security' => string 'Strict-Transport-Security' (length=25)
    'content-security-policy' => string 'Content-Security-Policy' (length=23)
    'transfer-encoding' => string 'Transfer-Encoding' (length=17)
    'content-type' => string 'Content-Type' (length=12)
    private 'protocol' => string '1.1' (length=3)
    private 'stream' =>
    object(GuzzleHttp\Psr7\Stream)[31]
    private 'stream' => resource(50, stream)
    private 'size' => int 990
    private 'seekable' => boolean true
    private 'readable' => boolean true
    private 'writable' => boolean true
    private 'uri' => string 'php://temp' (length=10)
    private 'customMetadata' =>
    array (size=0)
    empty
    C:\wamp64\www\assurimmat_v3\_web\ctrls\login_ctrl.php:49:
    object(GuzzleHttp\Psr7\Stream)[31]
    private 'stream' => resource(50, stream)
    private 'size' => int 990
    private 'seekable' => boolean true
    private 'readable' => boolean true
    private 'writable' => boolean true
    private 'uri' => string 'php://temp' (length=10)
    private 'customMetadata' =>
    array (size=0)
    empty

  4. #4
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 670
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 670
    Par défaut
    ça commence avec un code d'erreur 404 donc le soucis est peut-être là ?

  5. #5
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Octobre 2012
    Messages
    69
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Octobre 2012
    Messages : 69
    Par défaut
    justement puisque j'ai un erreur 404 (donc utilisateur non trouvé) je renvoie un message :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    echo json_encode(array("alert" => "User not found"));

  6. #6
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 670
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 670
    Par défaut
    je n'ai pas trop compris la logique de Guzzle par rapport à la gestion des erreurs 404 mais j'ai trouvé 2 façons de récupérer le corps de la réponse :
    soit
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $e->getResponse()->getBody()->seek(0);
    $corps = $e->getResponse()->getBody()->getContents();
    soit
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $corps = (string) $e->getResponse()->getBody();

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

Discussions similaires

  1. API REST de Google
    Par stade13 dans le forum APIs Google
    Réponses: 3
    Dernier message: 29/10/2011, 11h02
  2. [XML] API REST : comment ca marche ?
    Par chuckichucki dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 18/01/2011, 13h33
  3. [Authentification] API REstful PHP
    Par yoshï dans le forum REST
    Réponses: 1
    Dernier message: 22/07/2008, 09h33

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