How to post to GraphQL using PHP
P粉916760429
P粉916760429 2024-01-10 17:42:38
0
1
400

I make the following curl request to GraphQL. It works fine, but in production shell_exex is not allowed. How can I rewrite this curl post in valid PHP?

 $curl_string = 'curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer "' . AIRTABLE_API_KEY;
  $curl_second_string = ' -d \'{"query": "{fullCapaReview (id: \"' . $id . '\") {proposedRuleName submissionDate agencyContactName statusLawDept}}"}\' https://api.baseql.com/airtable/graphql/appXXXzzzzzzzzzz';
  $curl_complete_string = "$curl_string $curl_second_string";
  $result = shell_exec($curl_complete_string); 

Edit: Sorry, I entered the wrong query. The query that comes to my mind is:

' -d \'{"query": "{dMsAggency (agencyAcronym: \"' . $_agency . '\") {agencyAcronym fullCapaReview { id }}}"}\'

I made two similar calls. I'll leave the original there since someone answers accordingly.

This is what I have so far:

$curl = curl_init($url);
$query = 'query dMsAgencies($agencyAcronym: String) {agencyAcronym fullCapaReview { id }} ';
$variables = ["agencyAcronym" => $id ];
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['query' => $query, 'variables' => $variables])); 
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json','Authorization: Bearer ' . AIRTABLE_API_KEY]);
$response = curl_exec($curl);
curl_close($curl);


console_log("Response : " . $response);

This is the error message I received. I just want to see if my syntax is up to snuff.

Response : {"errors":[{"message":"Cannot query field \"agencyAcronym\" on type \"Query\".","locations":[{"line":1,"column":44}],"stack":["GraphQLError: Cannot query field \"agencyAcronym\" on type \"Query\"."," at Object.Field (/var/app/current/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:46:31)"," at Object.enter (/var/app/current/node_modules/graphql/language/visitor.js:323:29)"," at Object.enter (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:370:25)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]},{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}],"stack":["GraphQLError: Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\"."," at Object.leave (/var/app/current/node_modules/graphql/validation/rules/NoUnusedVariablesRule.js:38:33)"," at Object.leave (/var/app/current/node_modules/graphql/language/visitor.js:344:29)"," at Object.leave (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:390:21)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]}]}

message":"Unable to query field \"agencyAcronym\" on type \"Query\"

{"message":"Variable \"$agencyAcronym\" is never used in action \"dMsAggency\".","locations":[{"line":1,"column":19 }]

P粉916760429
P粉916760429

reply all(1)
P粉805535434

The query is not the same, but assuming you know, your PHP example also has syntax issues.

query dMsAgencies($agencyAcronym: String) {
    agencyAcronym 
    fullCapaReview { 
        id 
    }
}

If you compare this to the example in the docs (when using variables) you will find that you can see that you are not currently using the $agencyAcronym variable anywhere (and you may not have a query named agencyAcronym in its schema). Here's an example (using the query from the first snippet):

query dMsAgencies($agencyAcronym: String) {
    fullCapaReview (id: $agencyAcronym) {
        proposedRuleName 
        submissionDate
        agencyContactName
        statusLawDept
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!