PHP code example of chinayin / restapi-sdk

1. Go to this page and download the library: Download chinayin/restapi-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chinayin / restapi-sdk example snippets


// 如果是 composer 安装
// re_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");

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" }
}

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');