A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
I'm trying to run a simple GraphQL query using cUrl in PHP, why do I get the error {"errors":{"query":"Required parameter missing or invalid"}}, when I run a similar query through Postman I get the correct response
$url = 'https://'.$shop_url.'/admin/api/2023-01/graphql.json';
$query = '{shop { name }}';
$requestData = [
'query' => $query
];
$s = curl_init();
curl_setopt($s, CURLOPT_URL, $url);
curl_setopt($s, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($s, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($s, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token: '.$brand->getShopifyCustomAccessToken()));
curl_setopt($s, CURLOPT_POSTFIELDS, json_encode($requestData));
curl_setopt($s, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($s, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($s);
Hi Rediskapsts,
The error message you're getting suggests that there is a problem with your query parameter. Here are a couple things you could check to debug this:
Check if $brand->getShopifyCustomAccessToken()
is returning the correct token. If the token is invalid or expired, you might get errors.
You're setting the CURLOPT_HTTPHEADER
option twice, which means the second call is overriding the first one.'s possible that you're not sending the Content-Type
header correctly because of this. Try combining them into a single array:
curl_setopt($s, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Shopify-Access-Token: '.$brand->getShopifyCustomAccessToken()
));
Make sure that $query
is correctly formatted as a string. If there are special characters or newlines in it, you might need to escape them.
It's recommended to use application/graphql
as Content-Type
when sending a GraphQL query. So, you should change 'Content-Type: application/json'
to 'Content-Type: application/graphql'
.
The GraphQL query should be sent as raw body, not as a JSON encoded array. So, change json_encode($requestData)
to just $query
.
Here's how the corrected code might look like:
$url = 'https://'.$shop_url.'/admin/api/2023-01/graphql.json';
$query = '{shop { name }}';
$s = curl_init();
curl_setopt($s, CURLOPT_URL, $url);
curl_setopt($s, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($s, CURLOPT_HTTPHEADER, array(
'Content-Type: application/graphql',
'X-Shopify-Access-Token: '.$brand->getShopifyCustomAccessToken()
));
curl_setopt($s, CURLOPT_POSTFIELDS, $query);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
curl_setopt($s, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($s, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($s);
If you're still getting errors after trying these steps, it might be a good idea print out the full response from cURL to see if there's any additional about what's going wrong. You can do this by adding echo curl_error($s);
after curl_exec($s);
.
Hope this helps!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Hi Liam,
All of these tips are pretty obvious and none of them solve the problem: