繁体   English   中英

Php cURL 用 GRAPHQL 调用 API

[英]Php cURL with calling an API with GRAPHQL

我正在尝试调用名为Wave的 api 我以前使用过 cURL 但从未使用过 GRAPHQL 查询。 我想知道使用 cURL 时下面有什么问题。 我收到错误错误请求下面是我的代码示例。

这就是 API cURL 是什么

curl -X POST "https://reef.waveapps.com/graphql/public" \
  -H "Authorization: Bearer <ACCESS_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{ "query": "query { user { id defaultEmail } }" }'


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://reef.waveapps.com/graphql/public');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "query": "query { user { id defaultEmail } }');
curl_setopt($ch, CURLOPT_POST, 1);


$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer 1212121';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
var_dump($result);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

任何帮助都会有所帮助。

对于那些想要在没有第三方库的情况下查询 GraphQL 服务的人,我基本上采用了 Brian 的代码并针对我已经为其编写了 Node.js 代码的 GraphCMS 服务进行了测试。 所以我知道 url、授权令牌和查询都有效。

<?php
$endpoint = "https://api-euwest.graphcms.com/v1/[[your id number here]]/master";//this is provided by graphcms
$authToken = "[[your auth token]]";//this is provided by graphcms
$qry = '{"query":"query {products(where:{status:PUBLISHED}){title,img,description,costPrice,sellPrice,quantity,sku,categories {name},brand {name}}}"}';

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$authToken;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
var_dump($result);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

?>

一切正常。 auth token是GraphCMS提供的一个大长字符串,只需要在header中传递即可。 所以没有真正棘手的身份验证过程 - 只要你有令牌。

我可以推荐使用https://github.com/softonic/graphql-client ,它对我们很有用。

您可以创建自己的客户端,传递您想要的任何中间件:

$clientWithMiddleware = \MyGuzzleClientWithMiddlware::build();
$graphQLClient = new \Softonic\GraphQL\Client(
    $clientWithMiddleware,
    new \Softonic\GraphQL\ResponseBuilder()
);

有关如何使用中间件构建 Guzzle 客户端的示例,您可以查看: https : //github.com/softonic/guzzle-oauth2-middleware/blob/master/src/ClientBuilder.php

如果没有认证

您可以使用 file_get_contents 而不是 curl

$url = http://myapi/graphql?query={me{name}}

$html =file_get_contents($url);
echo $html;

在graphql的查询参数中使用json;

一种更简单的方法是使用 API 平台。 我经常使用 Postman,该平台具有在应用程序的 GraphQl 工具部分中为您提供 GraphQL 请求的 PHP cURL 代码的功能。

有点晚了,但我做了这段代码

$endpoint = "https://gql.waveapps.com/graphql/public";
$authToken = ""; //Your Bearer code
$qry = '{"query": "query {user {id firstName lastName defaultEmail createdAt modifiedAt}}"}';
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer '.$authToken;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
var_dump($result);

if (curl_errno($ch)) {
  echo 'Error:' . curl_error($ch);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM