我正在查看 Parse.com REST API 并使用 PHP 使用的 Curl 包装器进行调用。
原始卷曲代码(作品):
curl -X GET \
-H "X-Parse-Application-Id: myApplicationID" \
-H "X-Parse-REST-API-Key: myRestAPIKey" \
https://api.parse.com/1/classes/Steps
PhP代码(作品):
$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);
那很好而且很花哨,但是现在当我尝试添加查询约束时:
原始卷曲代码(作品):
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
唉,我们最终得出了我的问题——上述代码的 php 模拟是什么?
PHP代码(不起作用):
$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);
错误响应:
Object ( [code] => 107 [error] => invalid json: where%3D%7B%22steps%22%3A9243%7D )