Heim  >  Fragen und Antworten  >  Hauptteil

Stellen Sie mithilfe von PHP-GraphQL-Abfragen eine Verbindung zur Dutchie-API her

Ich habe einen Shop auf Dutchie.com. Ich möchte einen API-Schlüssel verwenden, um auf ihre Produkte zuzugreifen. Dies muss über die Dutchie-API unter Verwendung von in PHP integriertem GraphQL erfolgen.

Hier ist der Beispiel-API-Schlüssel:

public-eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJBUEktQ0xJRU5UIiwiZXhwIjozMzE4NjM5Mjc0NSwiaWF0IjoxNjI5NDgzOTQ1LCJpc3MiOiJodHRwczovL2R1dGNoY29tIiwianRpIjoiNGMtOTMyOC00MjhkLWEyYTMtOWQzMTc2ZTUwODY0IiwiZW50ZXJwcmlzZV9pZChLWExYTctNDM3OC05NWY4LTNlYzVzBiNSIsInV1aWQiOiI0M2ZlMjdkNy1iZWU2LTQxOTgtYWNhMi03N2Y5Y2I3MjI5MGIifQ.hCQWpcQ5uhKnZOSVQDA5SCMkx5kopC7H3upeU-1jMpg

Dies ist eine GraphQL-Ping-Mutation.

mutation Ping {
  ping {
    id,
    time
  }
}

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

http-Header-Parameter { „Autorisierung“: „Host-API-Schlüssel hier“ }

Ping-Ausgabe

Grundsätzlich möchte ich eine GraphQL-Abfrage auf meiner PHP-Seite ausführen. Ich werde es später zu meiner WordPress-Seite hinzufügen.

Ich habe die PHP-Bibliothek php-graphql-client ausprobiert. Kann mir jemand dabei helfen, dies mithilfe der oben genannten Bibliothek oder einer anderen zu tun, wäre ich wirklich dankbar. Ich habe zu viel Zeit damit verschwendet, weil ich sehr wenig über GraphQL wusste.

Das ist der Code, den ich ausprobiert habe.

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

Der Fehler, den ich bekommen habe. https://github.com/guzzle/psr7/blob/master/src/MessageTrait.php

P粉549986089P粉549986089179 Tage vor376

Antworte allen(1)Ich werde antworten

  • P粉648469285

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

    而不是:

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

    尝试

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

    Antwort
    0
  • StornierenAntwort