我正在查看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
$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模拟是什么?
$ch = curl_init('https://api.parse.com/1/classes/Steps'); $query = urlencode('where={"steps":9243}'); curl_setopt($ch,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 )
解决方法
您的上一个PHP示例已将请求从GET更改为POST.将参数传递给查询字符串而不是POST正文.尝试:
$query = urlencode('where={"steps":9243}'); $ch = curl_init('https://api.parse.com/1/classes/Steps?'.$query); curl_setopt( $ch,array( 'X-Parse-Application-Id: myApplicationID','Content-Type: application/json' ) ); curl_setopt($ch,1); curl_exec($ch); curl_close($ch);