我正在尝试调用一个名为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);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会有所帮助。
对于那些想要在没有第三方库的情况下查询 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);
}
?>
Run Code Online (Sandbox Code Playgroud)
一切正常。auth token是GraphCMS提供的一个大长字符串,只需要在header中传递即可。所以没有真正棘手的身份验证过程 - 只要你有令牌。
| 归档时间: |
|
| 查看次数: |
6024 次 |
| 最近记录: |