GraphQl查询使用PHP连接到荷兰API

发布于 2025-02-08 08:02:34 字数 2538 浏览 2 评论 0 原文

我在dutchie.com上有一家商店。我想使用API​​密钥访问其产品。 这必须通过与PHP集成的GraphQL通过Dutchie API进行。

这是样本API密钥:

public-eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJBUEktQ0xJRU5UIiwiZXhwIjozMzE4NjM5Mjc0NSwiaWF0IjoxNjI5NDgzOTQ1LCJpc3MiOiJodHRwczovL2R1dGNoY29tIiwianRpIjoiNGMtOTMyOC00MjhkLWEyYTMtOWQzMTc2ZTUwODY0IiwiZW50ZXJwcmlzZV9pZChLWExYTctNDM3OC05NWY4LTNlYzVzBiNSIsInV1aWQiOiI0M2ZlMjdkNy1iZWU2LTQxOTgtYWNhMi03N2Y5Y2I3MjI5MGIifQ.hCQWpcQ5uhKnZOSVQDA5SCMkx5kopC7H3upeU-1jMpg

这是GraphQl ping突变。

mutation Ping {
  ping {
    id,
    time
  }
}

荷兰终点: https://plus.dutchie.com/plus/plus/2021-07/graphql

{ “授权”:“在这里持有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']);

错误我得到的。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2025-02-15 08:02: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']
);

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']
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文