Querying API through Curl/PHP
api, curl, parse-platform, php
Solution
Your last PHP example has changed the request to a POST from a GET. Pass your parameters in the query string instead of the POST body. Try:
$query = urlencode('where={"steps":9243}');
$ch = curl_init('https://api.parse.com/1/classes/Steps?'.$query);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id: myApplicationID',
'X-Parse-REST-API-Key: myRestAPIKey',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
Problem
I'm looking at the Parse.com REST API and making calls using the Curl wrapper PHP uses. Raw Curl code(works): ``` curl -X GET \ -H "X-Parse-Application-Id: myApplicationID" \ -H "X-Parse-REST-API-Key: myRestAPIKey" \ https://api.parse.com/1/classes/Steps ``` PhP code(works): ``` $ch = curl_init('https://api.parse.com/1/classes/Steps'); curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id: myApplicationID', 'X-Parse-REST-API-Key: myRestAPIKey', 'Content-Type: application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); curl_close($ch); ``` Thats good and dandy, but now when I try to add a query constraint: Raw Curl code(works): ``` curl -X GET \ -H "X-Parse-Application-Id: myApplicationID" \ -H "X-Parse-REST-API-Key: myRestAPIKey" \ -G \ --data-urlencode 'where={"steps":9243}' \ https://api.parse.com/1/classes/Steps ``` Alas, we ultimately arrive at my question- What is the php analogue to the above code? PHP code(does not work): ``` $ch = curl_init('https://api.parse.com/1/classes/Steps'); $query = urlencode('where={"steps":9243}'); curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Parse-Application-Id: myApplicationID', 'X-Parse-REST-API-Key: myRestAPIKey', 'Content-Type: application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $query); curl_exec($ch); curl_close($ch); ``` Error response: ``` Object ( [code] => 107 [error] => invalid json: where%3D%7B%22steps%22%3A9243%7D ) ```