php CURL 400 bad request

patrick_j_rice
Community Member

Hi all

I'll a newbe to the world of API and graphQL, please be gentle.

I'm trying to use the graphQL endpoint with php. this is my code below. I can't use file_get_contents as I have to use CURL to work on our sever. 

My code is below, when I run this i'm getting 400 Bad request. what is the error in my code? how do I manage to consume these using PHH

 

 

<?php
$endpoint = "https://XXX.instructure.com/api/graphql";//this is provided by graphcms
$authToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";//this is provided by graphcms

$qry = "{
  allCourses {
    name
    assignmentsConnection {
      nodes {
        name
        dueAt
      }
    }
  }
}
";

$data = $qry;

echo "\n****************************************\n";
echo "\n*************Query Data***************************\n";
var_dump($data);


$url = $endpoint;


//Setting up CURL
$curl = curl_init($url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    "Accept: application/json",
    "Content-Type: application/json",
    "Authorization: Bearer ".$authToken,
    'Content-Length: '.strlen($data),
);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1); // Specify the request method as POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Set the posted fields
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // This will follow any redirects
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_VERBOSE, true);

$resp = curl_exec($curl);
curl_close($curl);
echo "\n****************************************\n";
echo "\n*************Responce***************************\n";
var_dump($resp);

?>

 

 

0 Likes