ndapio / rest-helper
支持与REST API通信的库
v1.0.3
2023-12-09 22:25 UTC
Requires
- php: *
- ext-json: *
- ext-simplexml: *
- guzzlehttp/guzzle: ^7.0.1
README
支持与REST API通信的库。此库可以加快您使用REST API的开发速度,并使其更容易进行故障排除。
- 内置代码库,用于与Guzzle交互以调用REST API。
- 内置代码,支持HTTP/HTTPS代理下的Guzzle请求。
- 内置代码,用于获取响应cookie。
- 内置代码,用于支持常见的认证方法:用户名和密码或访问令牌
- 内置代码,支持表单参数、JSON数据、XML数据
指南和示例
您可以将RestApi类扩展到您的项目中使用。
示例
<?php
use NDAPio\RestHelper\RestApi;
class SampleRestful extends RestApi {
public function __construct($base_url = "https://sample.com") {
$this->base_url = $base_url;
}
public function sample() {
$args = array(
"path" => "/path",
"params" => array(
"param_1" => "value_1",
),
"headers" => array(
"Header 1" => "Value 1"
),
"data_type" => "json:json", // format: input:output of REST API
// default if not defined: json:json
// json:json => REST API endpoint gets json and returns json
// form:json => REST API endpont gets form params (like HTTP Form post) and returns json
// json:xml => REST API endpoint gets json and returns xml
// form:xml => REST API endpont gets form params (like HTTP Form post) and returns xml
);
return $this->doGET($args); // doPOST, doPUT, doDELETE
}
}
?>
注意:在sample文件夹中有更多示例
响应
成功响应
array(
"status" => "success",
"time" => $timestamp,
"code" => $code,
"cookies" => $cookies,
"response" => $response_data_parsed // depend on the data_type input
);
错误响应
$response = array(
"status" => "error",
"time" => $timestamp,
"code" => $code // 401, 403, 500, ...
);
异常响应
array(
"status" => "exception",
"code" => 000,
"time" => $timestamp,
"response" => array(
"message" => $exception_message,
"action" => $action,
"headers" => $headers,
"params" => $params,
"proxy" => $proxy
)
);