chinayin / restapi-sdk
restapi sdk
Installs: 5 983
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 2
Requires
- php: >=7.2
- ext-curl: *
- ext-json: *
- ext-openssl: *
- guzzlehttp/guzzle: ^6.3|^7.0
- symfony/polyfill-php80: ^1.26
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^8.0|^9.0
- 1.x-dev
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.2
- v1.0.0
- 0.7.x-dev
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.8
- v0.6.7
- v0.6.6
- v0.6.5
- v0.6.4
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6
- v0.5.1
- v0.5
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4
- v0.3.4
- v0.3.3
- v0.3.1
- v0.3
- 0.2.7
- 0.2.6
- 0.2.5
- 0.2.4
- 0.2.3
- v0.2.2
- 0.2.1
- 0.2.0
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dependabot/github_actions/1.x/actions/cache-3
- dev-dependabot/github_actions/1.x/actions/checkout-3
This package is auto-updated.
Last update: 2024-12-10 04:28:03 UTC
README
安装
运行环境要求 PHP 7.2 及以上版本,以及 cURL。
composer 安装
如果使用标准的包管理器 composer,你可以很容易的在项目中添加依赖并下载:
composer require chinayin/restapi-sdk
初始化
完成上述安装后,需要对 SDK 初始化。 同时联系技术同学,获取SYS_ID 和 SECRET_KEY。 然后在项目中加载 SDK,并初始化:
REGION
// 如果是 composer 安装 // require_once("vendor/autoload.php"); // 如果是手动安装 require_once("vendor/restapi-sdk/src/autoload.php"); //// SSO使用下面这个 // 参数依次为 sys-id, secret-key, access-token RestAPI\Client::initialize("sys_id", "secret_key", "access_token"); //// SERVICE_API使用这个 // 参数依次为 sys-id, secret-key, region RestAPI\RestServiceClient::initialize("sys_id", "secret_key", "region");
使用示例
客户端请求
SsoAPI 服务
use RestAPI\Client; use RestAPI\CloudException; try { $response = Client::get('/api/oauth/get'); $response = Client::post('/api/oauth/checklogin',[]); $response = Client::put('/api/oauth/put',[]); $response = Client::delete('/api/oauth/delete',[]); } catch (CloudException $ex) { // 如果返回错误,这里会抛出异常 CloudException // 错误格式 错误码不为0都为报错 // { "error_code": 1, "message": "error" } }
ServiceAPI 服务
use RestAPI\RestServiceClient; use RestAPI\RestAPIException; try { $response = RestServiceClient::get('/api/oauth/get'); $response = RestServiceClient::post('/api/oauth/checklogin',[]); $response = RestServiceClient::put('/api/oauth/put',[]); $response = RestServiceClient::delete('/api/oauth/delete',[]); } catch (RestAPIException $ex) { // 如果返回错误,这里会抛出异常 RestServiceClient // 错误格式 错误码不为0都为报错 // { "error_code": 1, "message": "error" } // $ex->getData(); }
// 精简用法(须配置env) RestServicePost($path, $params, $headers = []) RestServiceGet($path, $params, $headers = []) $resp = RestServicePost('/api/oauth/checklogin',['username'=>'a']); // 超时时间设置(2s) $resp = RestServiceGet('/api/oauth/get',[],['timeout'=>2]); // 依照本地环境生成服务网址 $url = RestServiceBuildRequestUrl('/api/oauth/get');
env配置
[restapi]
sys_id = 1
secret_key = xxxx
region = xx
# 当需要单独配置连接域名时
# server_url = rest.xxxx.local
感谢
leancloud