mantoufan / yzhangateway
Developing PHP SDK for any API. 为任何 API 快速开发 PHP SDK.
Installs: 1 335
Dependents: 1
Suggesters: 0
Security: 0
Stars: 37
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.4
- mantoufan/yzhancache: ^1.0
- vlucas/phpdotenv: ^5.4
Requires (Dev)
- phpunit/phpunit: ^9.5
README
Developing PHP SDK for any API.
为任何 API 快速开发 PHP SDK.
Install 安装
composer require mantoufan/yzhangateway
Usage 使用
{root}/src
Client
- Create a
{client name}.php
insrc\Client
Directory - 在
src\Client
目录新建一个{客户端名称}.php
- Implement
request
method - 实现请求
request
方法
Auth
如需要,新建 .php
提供鉴权类
例如包含获取 authorization
请求头的方法
If necessary, create a new .php
to provide an authentication class,
such as a method to obtain the authorization
request header
Exception
如需要,新建 .php
声明新错误类型
If necessary, create a new .php
here to declare a new error type
Tool
ClientTool
提供 Request
静态方法,发出请求和响应
Provides the Request
static method for making requests and responses
{root}/tests
{root}/.env.test
运行 composer test
前,请将.env.test.template
重命名为.env.test
存放测试需要的变量
Before running composer test
, rename .env.test.template
to env.test
to store variables needed for testing
Example 示例
Common 通用
$yzhanGateway = new YZhanGateway('Common'); $res = $yzhanGateway->request(array( 'method' => 'GET', 'url' => 'https://animechan.vercel.app/api/random' ));
Use Cache 使用缓存
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 ) ));
Clear Cache 清理缓存
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 }
BaiduCloud 百度智能云
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']) ) ) ));
Cloudflare
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']) ) ));
Github
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' ));
OpenAI
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 ) ));
TencentCloud 腾讯云
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') ), ) ));