cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
patrick_j_rice
Community Member

php CURL 400 bad request

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 Kudos
1 Reply
patrick_j_rice
Community Member

Success:

removed the content type .... the apostrophe was causing me problems in the $qry

So I how have JSON coming back to me 🙂 

Thanks

 

 

 

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

$qry = "query= query {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(
    "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);

?>