Developing PHP SDK for any API.
为任何 API 快速开发 PHP SDK.
composer require mantoufan/yzhangateway
- Create a
{client name}.php
insrc\Client
Directory - 在
src\Client
目录新建一个{客户端名称}.php
- Implement
request
method - 实现请求
request
方法
如需要,新建 .php
提供鉴权类
例如包含获取 authorization
请求头的方法
If necessary, create a new .php
to provide an authentication class,
such as a method to obtain the authorization
request header
如需要,新建 .php
声明新错误类型
If necessary, create a new .php
here to declare a new error type
提供 Request
静态方法,发出请求和响应
Provides the Request
static method for making requests and responses
运行 composer test
前,请将.env.test.template
重命名为.env.test
存放测试需要的变量
Before running composer test
, rename .env.test.template
to env.test
to store variables needed for testing
$yzhanGateway = new YZhanGateway('Common');
$res = $yzhanGateway->request(array(
'method' => 'GET',
'url' => 'https://animechan.vercel.app/api/random'
));
Cache Results for 86400 seconds
$yzhanGateway = new YZhanGateway('Common');
$res = $yzhanGateway->cache()->request(array(
'method' => 'GET',
'url' => 'https://animechan.vercel.app/api/random',
'cache' => array(
'maxAge' => 86400
)
));
Cache Results for 86400 seconds
$yzhanGateway = new YZhanGateway('Common');
$params = array(
'method' => 'GET',
'url' => 'https://animechan.vercel.app/api/random',
'cache' => array(
'maxAge' => 86400
)
);
$res = $yzhanGateway->cache()->request($params);
if ($res === null) { // If results is bad, using getCache to get the yzhanCache instance
$yzhanGateway->getCache()->delete($yzhanGateway->getKey($params)); // Delete, set by the key
}
Purge Files by urls in Biadu Cloud CDN.
$yzhanGateway = new YZhanGateway('BaiduCloud', array(
'accessKey' => $_ENV['BAIDUCLOUD_ACCESSKEY'],
'secretKey' => $_ENV['BAIDUCLOUD_SECRETKEY']
));
$res = $yzhanGateway->request(array(
'method' => 'POST',
'url' => 'http://cdn.baidubce.com/v2/cache/purge',
'postFields' => array(
'tasks' => array(
array('url' => $_ENV['BAIDUCLOUD_TEST_URL'])
)
)
));
Purge Files by urls (<= 30) in Cloudflare.
$yzhanGateway = new YZhanGateway('Cloudflare', array(
'apiToken' => $_ENV['CLOUDFLARE_APITOKEN']
));
$res = $yzhanGateway->request(array(
'method' => 'POST',
'url' => 'https://api.cloudflare.com/client/v4/zones/' . $_ENV['CLOUDFLARE_REGION_ID'] . '/purge_cache',
'postFields' => array(
'files' => array($_ENV['CLOUDFLARE_TEST_URL'])
)
));
Get user's recent activities.
$yzhanGateway = new YZhanGateway('Github', array(
'accessToken' => $_ENV['GITHUB_ACCESS_TOKEN'],
'userAgent' => $_ENV['GITHUB_USER_NAME']
));
$res = $yzhanGateway->request(array(
'method' => 'GET',
'url' => 'https://api.github.com/users/' . $_ENV['GITHUB_USER_NAME'] . '/events'
));
Chat using text-davinci
$yzhanGateway = new YZhanGateway('OpenAI', array(
'apiKey' => $_ENV['OPENAI_APIKEY'],
// 'organization' => $_ENV['OPENAI_ORGANIZATION'] // Optional
));
$res = $yzhanGateway->request(array(
'method' => 'POST',
'url' => 'https://api.openai.com/v1/completions',
'postFields' => array(
'model' => 'text-davinci-003',
'prompt' => 'Hello',
'temperature'=> 0 // Optional, 0 means the most certain results
)
));
Get CVM list
$yzhanGateway = new YZhanGateway('TencentCloud', array(
'secretId' => $_ENV['TENCENTCLOUD_SECRET_ID'],
'secretKey' => $_ENV['TENCENTCLOUD_SECRET_KEY']
));
$res = $yzhanGateway->request(array(
'method' => 'POST',
'url' => 'https://cvm.tencentcloudapi.com',
'action' => 'DescribeInstances',
'version' => '2017-03-12',
'region' => 'ap-guangzhou',
'postFields' => array(
'Limit' => 1,
'Filters' => array(
array('Values' => array('未命名'), 'Name' => 'instance-name')
),
)
));