如何从api请求shopify graphql-admin-api? [英] How to request shopify graphql-admin-api from an api?
本文介绍了如何从api请求shopify graphql-admin-api?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的api请求shopify graphql-admin-api.我正在根据graphql-admin-api提供的文档进行此操作,但是它仍然会给我授权错误.
I am trying to request shopify graphql-admin-api from my api. I am doing it according to the documentation given by graphql-admin-api, but it still gives me authorization errors.
推荐答案
PHP用户可以使用此功能通过GraphQL向Shopify Admin API发出请求
PHP users can follow this function to make request to Shopify Admin API using GraphQL
我正在使用GuzzleHttp(PHP HTTP客户端)创建请求
I am using GuzzleHttp ( PHP HTTP client ) to create request
public function graph($query , $variables = []){
$domain = 'xxx.myshopify.com';
$url = 'https://'.$domain.'/admin/api/2019-10/graphql.json';
$request = ['query' => $query];
if(count($variables) > 0) { $request['variables'] = $variables; }
$req = json_encode($request);
$parameters['body'] = $req;
$stack = HandlerStack::create();
$client = new \GuzzleHttp\Client([
'handler' => $stack,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Shopify-Access-Token'=>$this->token // shopify app accessToken
],
]);
$response = $client->request('post',$url,$parameters);
return $body = json_decode($response->getBody(),true);
}
$query = "{ shop { name email } }"; // this is example graphQL query
$response = graph($query) // call this function
下面的代码可以帮助您检查此graphQL查询的费用
Below code can help you to check how much cost this graphQL query
$calls = $response->extensions->cost;
$apiCallLimitGraph = [
'left' => (int) $calls->throttleStatus->currentlyAvailable,
'made' => (int) ($calls->throttleStatus->maximumAvailable - $calls->throttleStatus->currentlyAvailable),
'limit' => (int) $calls->throttleStatus->maximumAvailable,
'restoreRate' => (int) $calls->throttleStatus->restoreRate,
'requestedCost' => (int) $calls->requestedQueryCost,
'actualCost' => (int) $calls->actualQueryCost,
];
这篇关于如何从api请求shopify graphql-admin-api?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文