通过Curl/PHP查询API
我正在查看Parse.com REST API,并使用PHP使用的Curl包装器进行调用 原始卷曲代码(工程): PhP代码(工程): 这很好,但现在当我尝试添加查询约束时: 原始卷曲代码(工程): 唉,我们最终得出了我的问题——与上述代码类似的php是什么 PHP代码(不工作): 错误响应:通过Curl/PHP查询API,php,api,curl,parse-platform,Php,Api,Curl,Parse Platform,我正在查看Parse.com REST API,并使用PHP使用的Curl包装器进行调用 原始卷曲代码(工程): PhP代码(工程): 这很好,但现在当我尝试添加查询约束时: 原始卷曲代码(工程): 唉,我们最终得出了我的问题——与上述代码类似的php是什么 PHP代码(不工作): 错误响应: Object ( [code] => 107 [error] => invalid json: where%3D%7B%22steps%22%3A9243%7D ) 上一个PHP示例已将请求
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,
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_setopt($ch, CURLOPT_POSTFIELDS, $query);
正在尝试设置请求正文,该正文对GET请求无效。cURL似乎允许您在GET请求()上设置主体
看起来您的PHP没有发出POST请求(至少从使用curl\u setopt($ch,CURLOPT\u POST,count($fields));
的其他PHP示例可以看出这一点。我相信您需要将数组传递给postfields选项:
$fields = array(
'where' => urlencode('{"steps":9243}')
);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
试试这个:
$query = json_encode(
array(
'where' => array( 'steps' => 9243 )
)
);
我从中收集到了这一点,但没有经过测试!Python示例似乎在发送查询之前对查询进行JSON编码,因此可能值得一试。要调用GET、POST、DELETE、PUT所有类型的请求,我创建了一个通用函数
define("SITEURL", "http://localhost:82/slimdemo/RESTAPI");
function CallAPI($method, $api, $data, $headers) {
$url = SITEURL . "/" . $api;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
switch ($method) {
case "GET":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "POST":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
break;
case "PUT":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
break;
}
$response = curl_exec($curl);
$data = json_decode($response);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check the HTTP Status code
switch ($httpCode) {
case 200:
$error_status = "200: Success";
return ($data);
break;
case 404:
$error_status = "404: API Not found";
break;
case 500:
$error_status = "500: servers replied with an error.";
break;
case 502:
$error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!";
break;
case 503:
$error_status = "503: service unavailable. Hopefully they'll be OK soon!";
break;
default:
$error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl);
break;
}
curl_close($curl);
echo $error_status;
die;
}
调用DeleteAPI
电话邮递
调用GetAPI
调用PutAPI
无耻的插件:如果您使用Runscope URL,您可以看到代码生成的HTTP请求/响应来回移动。很漂亮,谢谢!我对curl行--data urlencode(文档中说它用于发布数据)感到困惑。但是在查询中附加url是有意义的。关于--data参数,这是正确的,但是-G参数强制它返回GET。
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$fields = array(
'where' => urlencode('{"steps":9243}')
);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$query = json_encode(
array(
'where' => array( 'steps' => 9243 )
)
);
define("SITEURL", "http://localhost:82/slimdemo/RESTAPI");
function CallAPI($method, $api, $data, $headers) {
$url = SITEURL . "/" . $api;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
switch ($method) {
case "GET":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "POST":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
break;
case "PUT":
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
break;
}
$response = curl_exec($curl);
$data = json_decode($response);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check the HTTP Status code
switch ($httpCode) {
case 200:
$error_status = "200: Success";
return ($data);
break;
case 404:
$error_status = "404: API Not found";
break;
case 500:
$error_status = "500: servers replied with an error.";
break;
case 502:
$error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!";
break;
case 503:
$error_status = "503: service unavailable. Hopefully they'll be OK soon!";
break;
default:
$error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl);
break;
}
curl_close($curl);
echo $error_status;
die;
}
$data = array('id'=>$_GET['did']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('DELETE', "DeleteCategory", $data, $header);
$data = array('title'=>$_POST['txtcategory'],'description'=>$_POST['txtdesc']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('POST', "InsertCategory", $data, $header);
$data = array('id'=>$_GET['eid']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('GET', "GetCategoryById", $data, $header);
$data = array('id'=>$_REQUEST['eid'],m'title'=>$_REQUEST['txtcategory'],'description'=>$_REQUEST['txtdesc']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('POST', "UpdateCategory", $data, $header);