Sumith Harshan
Sumith Harshan

Reputation: 6445

GraphQL query connect to Dutchie API using PHP

I have a store in Dutchie.com. I want to access it's products using API key. This has to do via Dutchie API using GraphQL integrated with PHP.

This is the Sample API Key:

public-eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJBUEktQ0xJRU5UIiwiZXhwIjozMzE4NjM5Mjc0NSwiaWF0IjoxNjI5NDgzOTQ1LCJpc3MiOiJodHRwczovL2R1dGNoY29tIiwianRpIjoiNGMtOTMyOC00MjhkLWEyYTMtOWQzMTc2ZTUwODY0IiwiZW50ZXJwcmlzZV9pZChLWExYTctNDM3OC05NWY4LTNlYzVzBiNSIsInV1aWQiOiI0M2ZlMjdkNy1iZWU2LTQxOTgtYWNhMi03N2Y5Y2I3MjI5MGIifQ.hCQWpcQ5uhKnZOSVQDA5SCMkx5kopC7H3upeU-1jMpg

This is the GraphQL Ping mutation.

mutation Ping {
  ping {
    id,
    time
  }
}

Dutchie End Point: https://plus.dutchie.com/plus/2021-07/graphql

http header parameter { "Authorization":"Bearer API KEY HERE" }

Ping output enter image description here

Basically I want to run GraphQL query in my PHP page. I'll add into my WordPress page later.

I have tried php-graphql-client php library. Can someone help me to do this using above library or another one really appreciate. I wasted too much time for this as I have only few knowledge of GraphQL.

This is the code what I have tried.

$client = new Client(
    'https://plus.dutchie.com/plus/2021-07/graphql',
    ['Authorization => Bearer API Key here']
);
 

 
// Create the GraphQL mutation
$gql = (new Mutation('Ping'))
    ->setSelectionSet(
        [
            'id',
            'time',
        ]
    );

// Run query to get results
try {
    $results = $client->runQuery($gql);
}
catch (QueryError $exception) {

    // Catch query error and desplay error details
    print_r($exception->getErrorDetails());
    exit;
}

// Display original response from endpoint
var_dump($results->getResponseObject());

// Display part of the returned results of the object
var_dump($results->getData()->pokemon);

// Reformat the results to an array and get the results of part of the array
$results->reformatResults(true);
print_r($results->getData()['data']);

Error what I got. https://github.com/guzzle/psr7/blob/master/src/MessageTrait.php enter image description here

Upvotes: 0

Views: 809

Answers (1)

Alexis Dichosa
Alexis Dichosa

Reputation: 11

Instead of:

$client = new Client(
    'https://plus.dutchie.com/plus/2021-07/graphql',
    ['Authorization => Bearer API Key here']
);

Try

$client = new Client(
    'https://plus.dutchie.com/plus/2021-07/graphql',
    ['Authorization' => 'Bearer API Key here']
);

Upvotes: 1

Related Questions