我有下面的代码,以使一个GET语句使用PHP解析服务器的REST API:
$query = json_encode(
array(
'where' => array( 'userid' => "8728792347239" )
));
echo $query;
$ch = curl_init('https://*hidden*.herokuapp.com/parse/classes/computers?'.$query);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array(
'X-Parse-Application-Id: *hidden*',
'X-Parse-REST-API-Key: *hidden*',
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
print_r($response);
但是我收到以下错误:
{"code":102,"error":"Invalid parameter for query: {\"where\":{\"userid\":\"8728792347239\"}}"}
我究竟做错了什么?
而不必阅读文档,我敢打赌,它应该是URL编码,而不是JSON编码 - 或 - 该数据应该是在POST身体,而不是URL查询。
如果猜测#1是正确的,那么你的问题是,你正在使用json_encode
代替http_build_query
如
$query = http_build_query(
array(
'where' => array( 'userid' => "8728792347239" )
));
如果猜测#2是正确的,那么你的问题是,你将数据添加到URL查询,而不是将它添加到请求主体,如
$ch = curl_init('https://*hidden*.herokuapp.com/parse/classes/computers');
curl_setopt($ch,CURLOPT_POSTFIELDS,$query);