通过 Curl/PHP 查询 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18026258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Querying API through Curl/PHP
提问by JRam13
I'm looking at the Parse.com REST API and making calls using the Curl wrapper PHP uses.
我正在查看 Parse.com REST API 并使用 PHP 使用的 Curl 包装器进行调用。
Raw Curl code(works):
原始卷曲代码(有效):
curl -X GET \
-H "X-Parse-Application-Id: myApplicationID" \
-H "X-Parse-REST-API-Key: myRestAPIKey" \
https://api.parse.com/1/classes/Steps
PhP code(works):
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);
Thats good and dandy, but now when I try to add a query constraint:
那太好了,但现在当我尝试添加查询约束时:
Raw Curl code(works):
原始卷曲代码(有效):
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
Alas, we ultimately arrive at my question- What is the php analogue to the above code?
唉,我们最终得出了我的问题——与上述代码类似的 php 是什么?
PHP code(does not work):
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);
Error response:
错误响应:
Object ( [code] => 107 [error] => invalid json: where%3D%7B%22steps%22%3A9243%7D )
回答by Matt S
Your last PHP example has changed the request to a POST from a GET. Pass your parameters in the query string instead of the POST body. Try:
您的最后一个 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);
回答by Juned Ansari
To call GET,POST,DELETE,PUT All kind of request, i have created one common function
要调用 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;
}
CALL DeleteAPI
调用删除API
$data = array('id'=>$_GET['did']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('DELETE', "DeleteCategory", $data, $header);
CALL PostAPI
调用 PostAPI
$data = array('title'=>$_POST['txtcategory'],'description'=>$_POST['txtdesc']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('POST', "InsertCategory", $data, $header);
CALL GetAPI
调用获取API
$data = array('id'=>$_GET['eid']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('GET', "GetCategoryById", $data, $header);
CALL PutAPI
调用 PutAPI
$data = array('id'=>$_REQUEST['eid'],m'title'=>$_REQUEST['txtcategory'],'description'=>$_REQUEST['txtdesc']);
$header = array('USERTOKEN:' . GenerateToken());
$result = CallAPI('POST', "UpdateCategory", $data, $header);
回答by John Sheehan
This line:
这一行:
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
is trying to set a request body, which is not valid for a GET request. cURL appears to let you set a body on a GET request (example).
正在尝试设置对 GET 请求无效的请求正文。cURL 似乎可以让您在 GET 请求上设置主体(示例)。
It looks like your PHP is not making a POST request (at least best I can tell from looking at other PHP examples that use curl_setopt($ch,CURLOPT_POST, count($fields));
. I believe you need to pass an array to the postfields option:
看起来您的 PHP 没有发出 POST 请求(至少我可以通过查看其他使用curl_setopt($ch,CURLOPT_POST, count($fields));
.
$fields = array(
'where' => urlencode('{"steps":9243}')
);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);