我在 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
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
而不是:
$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'] );