将CURL命令行转换为PHP

时间:2014-07-17 14:46:30

标签: php parsing curl command-line converter

如何转换为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>

我试过了:

$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);

但结果是: bool(false)

1 个答案:

答案 0 :(得分:0)

同时删除CURLOPT_CUSTOMREQUESTCURLOPT_POSTCURLOPT_POSTFIELDS,并将在postfields中传递的数据附加到CURLOPT_URL。 尝试这个代码。

$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