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
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:
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.
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);.
All of these tips are pretty obvious and none of them solve the problem:
$brand->getShopifyCustomAccessToken() is correct and works fine with other REST API requests, and I also assume that I would receive an error that would indicate that the token is invalid.
Also works great with other REST API requests, but I combined them into one array and this did not affect the result.
As you can see in my example there are no line breaks or special characters.
I tried to use also ‘Content-Type: application/graphql’, it did not affect the result.
‘json_encode($requestData)’ returns a string which is valid JSON, but I also tried passing $query with the values ‘{shop { name }}’, ‘{“query”:“{shop { name }}”}’ and ‘{“query”: “query { shop { name } }”}’ it didn’t help.