GET API,用于解析服务器与Where子句PHP

问题描述 投票:0回答:1

我有下面的代码,以使一个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\"}}"}

我究竟做错了什么?

php rest curl parse-server
1个回答
0
投票

而不必阅读文档,我敢打赌,它应该是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);
© www.soinside.com 2019 - 2024. All rights reserved.