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 Discussion :

SF 5.4.9 - Consommer une api REST GLPI


Sujet :

PHP

  1. #1
    Membre régulier Avatar de Mika2008
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    176
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 176
    Points : 71
    Points
    71
    Par défaut SF 5.4.9 - Consommer une api REST GLPI
    Bonjour,

    j'aimerais consommer une api REST qui à la documentation suivante :


    Pièce jointe 621090

    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
    Init session
     
        URL: apirest.php/initSession/
        Description: Request a session token to uses other API endpoints.
        Method: GET
     
        Parameters: (Headers)
            App-Token: authorization string provided by the GLPI API configuration. Optional.
            a couple login & password: 2 parameters to login with user authentication. You should pass this 2 parameters in http basic auth. It consists in a Base64 string with login and password separated by ":" A valid Authorization header is:
                "Authorization: Basic base64({login}:{password})"
     
            OR
     
            an user_token defined in User Preference (See 'Remote access key') You should pass this parameter in 'Authorization' HTTP header. A valid Authorization header is:
                "Authorization: user_token q56hqkniwot8wntb3z1qarka5atf365taaa2uyjrn"
        Parameters: (query string)
            get_full_session (default: false): Get the full session, useful if you want to login and access session data in one request.
        Returns:
            200 (OK) with the session_token string.
            400 (Bad Request) with a message indicating an error in input parameter.
            401 (UNAUTHORIZED)
     
    Examples usage (CURL):
     
    $ curl -X GET \
    -H 'Content-Type: application/json' \
    -H "Authorization: Basic Z2xwaTpnbHBp" \
    -H "App-Token: f7g3csp8mgatg5ebc5elnazakw20i9fyev1qopya7" \
    'http://path/to/glpi/apirest.php/initSession'
     
    < 200 OK
    < {
       "session_token": "83af7e620c83a50a18d3eac2f6ed05a3ca0bea62"
    }
     
    $ curl -X GET \
    -H 'Content-Type: application/json' \
    -H "Authorization: user_token q56hqkniwot8wntb3z1qarka5atf365taaa2uyjrn" \
    -H "App-Token: f7g3csp8mgatg5ebc5elnazakw20i9fyev1qopya7" \
    'http://path/to/glpi/apirest.php/initSession?get_full_session=true'
     
    < 200 OK
    < {
       "session_token": "83af7e620c83a50a18d3eac2f6ed05a3ca0bea62",
       "session": {
          'glpi_plugins': ...,
          'glpicookietest': ...,
          'glpicsrftokens': ...,
          ...
       }
    }
    J'ai donc créer un service dans GLPI :


    src/Service/CallApiService.php


    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
    <?php
     
    namespace App\Service;
     
    use Symfony\Contracts\HttpClient\HttpClientInterface;
     
    class CallApiService
    {
        private $client;
     
        public function __construct(HttpClientInterface $client)
        {
            $this->client = $client;
        }
     
        public function getFranceData(): array
        {
            $response = $this->client->request(
                'GET',
                'https://serveurweb/glpi/apirest.php/initSession/',
                // these values are automatically encoded before including them in the URL
                ['headers' => [
                    'Content-Type:' => 'application/json',
                    'Authorization' => 'UTILISATEUR MOTDEPASSE!!'
                ],]
            );
     
            dump($this->client);
     
     
            try {
                $response->toArray();
            } catch (\Exception $e) {
                dump($e->getMessage());
                return array("message" => "Impossible requeteur API rest GLPI");
            }
        }
    }
    j'ai donc le message retour d'erreur suivant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    "HTTP/1.1 400 Bad Request returned for "https://serveurweb/glpi/apirest.php/initSession/"."

    avec l'entete suivante :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     	https://serveurweb/glpi/apirest.php/initSession/
     
    ["headers" => ["Content-Type:" => "application/json"
        "Authorization" => "UTILISATEUR MOTDEPASSE!!"
      ]
    ]

    je pense que l'entete est correcte je ne vois pas se qui cloche dans l'appel

    qu'en pensez vous svp ?

  2. #2
    Membre régulier Avatar de Mika2008
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    176
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2007
    Messages : 176
    Points : 71
    Points
    71
    Par défaut
    voici la bon code :


    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
    <?php
     
    namespace App\Service;
     
    use Symfony\Contracts\HttpClient\HttpClientInterface;
     
    class CallApiService
    {
        private $client;
     
        public function __construct(HttpClientInterface $client)
        {
            $this->client = $client;
        }
     
        public function getFranceData(): array
        {
     
            $string =  'Basic ' . base64_encode('Utilisateur:PASSORWD');
     
     
            $response = $this->client->request(
                'GET',
                'https://srvweb/glpi/apirest.php/initSession/',
                // these values are automatically encoded before including them in the URL
                ['headers' => [
                    'Content-Type:' => 'application/json',
                    'Authorization' => $string,
     
     
                ],]
            );
     
            try {
                return $response->toArray();
            } catch (\Exception $e) {
                dump($e->getMessage());
                return array("message" => "Impossible requeteur API rest GLPI", "response" => $response);
            }
        }
    }

    je met en résolu

    merci à tous !!

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

Discussions similaires

  1. Consommer une API REST
    Par CharleLéo dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 07/09/2016, 09h32
  2. Sécurisation d'une API REST
    Par yeste64 dans le forum REST
    Réponses: 0
    Dernier message: 05/07/2014, 11h02
  3. Appeler une api Rest depuis un serveur d'application
    Par marc11 dans le forum Java EE
    Réponses: 1
    Dernier message: 25/02/2013, 10h25
  4. [2.x] Construire une API Rest
    Par noepk dans le forum Symfony
    Réponses: 1
    Dernier message: 14/09/2012, 15h43
  5. Réponses: 5
    Dernier message: 09/12/2011, 08h21

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