Brian Nowlan
Brian Nowlan

Reputation: 147

Php cURL with calling an API with GRAPHQL

I am trying to call an api called Wave I have used cURL before but never with GRAPHQL queries. I am wondering what is wrong with the below when using cURL. I get an error Bad Request Below is an exmple of my code.

This is what the API cURL is

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);
}

Any help would be helpful.

Upvotes: 9

Views: 14412

Answers (6)

Steve Mc
Steve Mc

Reputation: 3441

For those wanting to query a GraphQL service WITHOUT a third party library, I basically took Brian's code and tested against a GraphCMS service I had already written Node.js code for. So I knew the url, authorization token, and query all worked.

<?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);
}

?>

All worked good. The auth token is a big long character string provided by GraphCMS and only needs to be passed in the header. So no real tricky authentication process - as long as you have the token.

Upvotes: 14

Bit late but I made this code

$endpoint = "https://gql.waveapps.com/graphql/public";
$authToken = ""; //Your Bearer code
$qry = '{"query": "query {user {id firstName lastName defaultEmail createdAt modifiedAt}}"}';
$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, true);
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);
}

Upvotes: 0

Edwin Broce
Edwin Broce

Reputation: 71

A way easier way to go about doing this is by using an API platform. I often use Postman, the platform have the functionality to give you the PHP cURL code for a GraphQL request in the GraphQl tools part of the application.

Upvotes: 3

Ucdemir
Ucdemir

Reputation: 3098

If there is no authentication

You can use file_get_contents instead of curl

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

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

use json in query paramter for graphql;

Upvotes: 0

milo5b
milo5b

Reputation: 160

You can create your own client passing whatever middleware you'd like:

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

For an example how to build a Guzzle client with middleware you can check this out: https://github.com/softonic/guzzle-oauth2-middleware/blob/master/src/ClientBuilder.php

Upvotes: 0

spawnia
spawnia

Reputation: 909

I can recommend using https://github.com/softonic/graphql-client, it has worked great for us.

Upvotes: 3

Related Questions