search

Home  >  Q&A  >  body text

Connect to the Dutchie API using PHP GraphQL queries

I have a store on Dutchie.com. I want to use an API key to access their products. This must be done through the Dutchie API using GraphQL integrated with PHP.

This is a sample API key:

public-eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJBUEktQ0xJRU5UIiwiZXhwIjozMzE4NjM5Mjc0NSwiaWF0IjoxNjI5NDgzOTQ1LCJpc3MiOiJodHRwczovL2R1dGNoY29tIiwianRpIjoiNGMtOTMyOC00MjhkLWEyYTMtOWQzMTc2ZTUwODY0IiwiZW50ZXJwcmlzZV9pZChLWExYTctNDM3OC05NWY4LTNlYzVzBiNSIsInV1aWQiOiI0M2ZlMjdkNy1iZWU2LTQxOTgtYWNhMi03N2Y5Y2I3MjI5MGIifQ.hCQWpcQ5uhKnZOSVQDA5SCMkx5kopC7H3upeU-1jMpg

This is a GraphQL Ping mutation.

mutation Ping {
  ping {
    id,
    time
  }
}

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

http header parameters { "Authorization": "Host API key here" }

Ping output

Basically I want to run a GraphQL query in my PHP page. I will add it to my WordPress page later.

I tried php-graphql-client php library. Can someone help me to do this using the above library or another would be really appreciated. I wasted too much time on this because I knew very little about GraphQL.

This is the code I 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']);

The error I get. https://github.com/guzzle/psr7/blob/master/src/MessageTrait.php

P粉549986089P粉549986089245 days ago452

reply all(1)I'll reply

  • P粉648469285

    P粉6484692852024-03-28 14:04:34

    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']
    );

    reply
    0
  • Cancelreply