Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php cURL with calling an API with GRAPHQL

Tags:

php

curl

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.

like image 804
Brian Nowlan Avatar asked Mar 01 '19 09:03

Brian Nowlan


People also ask

Can I use GraphQL with PHP?

In this blog, we will learn about the GraphQL in PHP. Firstly we should know, in any language to use GraphQL we have to import their library. In PHP, we can use this library file: webonyx/graphql-php.

Can GraphQL call REST API?

GraphQL serves as a welcome alternative to RESTful API design to further improve client-server interactions and how your front-end and back-end teams collaborate. Learn how the two differ and why more and more developers are using GraphQL. There is no right kind of architecture for data exchange.

Can I use curl in PHP?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.


3 Answers

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 fine. 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.

like image 139
Steve Mc Avatar answered Oct 05 '22 19:10

Steve Mc


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

like image 40
spawnia Avatar answered Oct 05 '22 19:10

spawnia


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.

like image 29
Edwin Broce Avatar answered Oct 05 '22 19:10

Edwin Broce