6
votes

Php cURL avec appel d'une API avec GRAPHQL

J'essaie d'appeler une API appelée Wave J'ai déjà utilisé cURL mais jamais avec les requêtes GRAPHQL. Je me demande ce qui ne va pas avec ce qui suit lors de l'utilisation de cURL. J'obtiens une erreur Mauvaise demande Voici un exemple de mon code.

Voici ce qu'est l'API cURL

curl -X POST "https://reef.waveapps.com/graphql/public" \
  -H "Authorization: Bearer <ACCESS_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{ "query": "query { user { id defaultEmail } }" }'


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://reef.waveapps.com/graphql/public');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "query": "query { user { id defaultEmail } }');
curl_setopt($ch, CURLOPT_POST, 1);


$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer 1212121';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
var_dump($result);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

Toute aide serait utile.


0 commentaires

4 Réponses :


3
votes

Je peux vous recommander d'utiliser https://github.com/softonic/graphql-client , cela a très bien fonctionné pour nous.


3 commentaires

Cela fonctionne bien mais j'utilise un jeton d'accès et il ne nécessite pas OAuth2 Vous ne savez pas si vous avez le processus sem?


Je n'ai pas encore traité d'authentification, car notre API est uniquement interne.


Dans cette bibliothèque, il y a un générateur d'aide qui utilise le middleware guzzle OAuth2. Cependant, vous pouvez utiliser votre propre middleware guzzle de la même manière: github.com/softonic/graphql-client/blob/master/src/...



0
votes

Vous pouvez créer votre propre client en passant le middleware de votre choix:

$clientWithMiddleware = \MyGuzzleClientWithMiddlware::build();
$graphQLClient = new \Softonic\GraphQL\Client(
    $clientWithMiddleware,
    new \Softonic\GraphQL\ResponseBuilder()
);

Pour un exemple de création d'un client Guzzle avec un middleware, vous pouvez consulter ceci: https://github.com/softonic/guzzle- oauth2-middleware / blob / master / src / ClientBuilder.php


0 commentaires

7
votes

Pour ceux qui veulent interroger un service GraphQL SANS bibliothèque tierce, j'ai essentiellement pris le code de Brian et testé contre un service GraphCMS pour lequel j'avais déjà écrit du code Node.js. Je savais donc que l'URL, le jeton d'autorisation et la requête fonctionnaient tous.

<?php
$endpoint = "https://api-euwest.graphcms.com/v1/[[your id number here]]/master";//this is provided by graphcms
$authToken = "[[your auth token]]";//this is provided by graphcms
$qry = '{"query":"query {products(where:{status:PUBLISHED}){title,img,description,costPrice,sellPrice,quantity,sku,categories {name},brand {name}}}"}';

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$authToken;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
var_dump($result);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

?>

Tout fonctionnait bien. Le jeton d'authentification est une grande chaîne de caractères longue fournie par GraphCMS et ne doit être transmise que dans l'en-tête. Donc pas de véritable processus d'authentification délicat - tant que vous avez le jeton.


0 commentaires

0
votes

S'il n'y a pas d'authentification

Vous pouvez utiliser file_get_contents au lieu de curl

$url = http://myapi/graphql?query={me{name}}

$html =file_get_contents($url);
echo $html;

utiliser json dans le paramètre de requête pour graphql;


0 commentaires