0

I am trying to execute this curl command via php but I'm unable to send the urlencode data with GET request

This is my curl command

curl -X GET \
-H "X-Parse-Application-Id: ..." \
-H "X-Parse-REST-API-Key: ..." \
-G \
--data-urlencode "where={\"createdAt\": \"2016-01-07T20:38:02.428Z\"}" \
https://api.parse.com/1/classes/Books';

This is my php code till yet

<?php

header('Content-Type: application/json');

$ParseAppID = "X-Parse-Application-Id: ... " ;
$ParseRestKey = "X-Parse-REST-API-Key: ... " ;
//$ParseMasterKey = ;
$GET ="GET";

$data = array("createdAt"=>"2016-01-09T08:42:36.675Z");
$data_string = json_encode($data);
$curl = curl_init();

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $GET);
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($ParseAppID, $ParseRestKey));
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode('where='.$data_string));
curl_setopt($curl, CURLOPT_URL, "https://api.parse.com/1/classes/Text");

$result = curl_exec( $curl );

curl_close( $curl );
echo $result;
?>

1 Answer 1

1

As far as I know there is no equivalent for POSTFIELDS for a get request so you have to add it as a parameter on the url instead.

<?php

header('Content-Type: application/json');

$ParseAppID = "X-Parse-Application-Id: ... " ;
$ParseRestKey = "X-Parse-REST-API-Key: ... " ;
//$ParseMasterKey = ;
$GET ="GET";

$data = array("createdAt"=>"2016-01-09T08:42:36.675Z");
$data_string = json_encode($data);
$curl = curl_init();

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $GET);
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($ParseAppID, $ParseRestKey));
curl_setopt($curl, CURLOPT_URL, "https://api.parse.com/1/classes/Text?where=".urlencode($data_string));

$result = curl_exec( $curl );

curl_close( $curl );
echo $result;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude .... totally forgot that option.... also works even if we don't use 'urlencode()'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.