user3799878 user3799878 - 4 months ago 56
PHP Question

Convert CURL command line to PHP

How can I convert to PHP Curl?

curl -X GET \<br>
-H "X-Parse-Application-Id: APPIDKEY" \<br>
-H "X-Parse-REST-API-Key: RESTAPIKEY" \<br>
-G \<br>
data-urlencode 'where={"playerName":"Sean Plott"' \<br>
https://api.parse.com/1/classes/ClassName<br>


I have tried:

$ch = curl_init();<br>
curl_setopt($ch, CURLOPT_URL, "https://api.parse.com/1/classes/ClassName");<br>
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');<br>
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Parse-Application-Id: APPIDKEY","X-Parse- REST-API-Key: RESTAPIKEY"));<br>
curl_setopt($ch, CURLOPT_POST, true);<br>
curl_setopt($ch, CURLOPT_POSTFIELDS, 'where={"playerName":"Sean Plott"}');<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>
$player = curl_exec($ch);<br>
curl_close($ch);<br>
var_dump($player);


but the result is:bool(false)!

Answer

Also remove the CURLOPT_CUSTOMREQUEST and CURLOPT_POST, CURLOPT_POSTFIELDS and append the data passed in postfields to CURLOPT_URL. TRY THIS CODE.

$data = 'where={"playerName":"Sean Plott"}';
$url = 'https://api.parse.com/1/classes/ClassName';
$requestUrl = $url . '?' . url_encode($data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, 
  array(
    "X-Parse-Application-Id: APPIDKEY",
    "X-Parse-REST-API-Key: RESTAPIKEY"
  )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$player = curl_exec($ch);
curl_close($ch);
var_dump($player);

HTH,

VR