首頁  >  問答  >  主體

使用 PHP GraphQL 查詢連線到 Dutchie API

我在 Dutchie.com 上有一家商店。我想使用 API 金鑰存取其產品。 這必須透過 Dutchie API 使用與 PHP 整合的 GraphQL 來完成。

這是範例 API 金鑰:

public-eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJBUEktQ0xJRU5UIiwiZXhwIjozMzE4NjM5Mjc0NSwiaWF0IjoxNjI5NDgzOTQ1LCJpc3MiOiJodHRwczovL2R1dGNoY29tIiwianRpIjoiNGMtOTMyOC00MjhkLWEyYTMtOWQzMTc2ZTUwODY0IiwiZW50ZXJwcmlzZV9pZChLWExYTctNDM3OC05NWY4LTNlYzVzBiNSIsInV1aWQiOiI0M2ZlMjdkNy1iZWU2LTQxOTgtYWNhMi03N2Y5Y2I3MjI5MGIifQ.hCQWpcQ5uhKnZOSVQDA5SCMkx5kopC7H3upeU-1jMpg

這是 GraphQL Ping 突變。

mutation Ping {
  ping {
    id,
    time
  }
}

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

#http標頭參數 { “授權”:“此處為承載 API 金鑰” }

Ping 輸出

基本上我想在我的 PHP 頁面中執行 GraphQL 查詢。我稍後會添加到我的 WordPress 頁面中。

我嘗試過 php-graphql-client php 函式庫。 有人可以幫助我使用上面的庫來做到這一點,或者另一個人真的很感激。我為此浪費了太多時間,因為我對 GraphQL 的了解很少。

這是我嘗試過的程式碼。

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

我得到的錯誤。 https://github.com/guzzle/psr7/blob/master/src/MessageTrait.php

P粉549986089P粉549986089179 天前374

全部回覆(1)我來回復

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

    回覆
    0
  • 取消回覆