mantoufan / yzhangateway
为任何 API 快速开发 PHP SDK。
1.0.4
2023-05-03 11:16 UTC
Requires
- php: >=5.4
- mantoufan/yzhancache: ^1.0
- vlucas/phpdotenv: ^5.4
Requires (Dev)
- phpunit/phpunit: ^9.5
README
为任何 API 快速开发 PHP SDK。
为任何 API 快速开发 PHP SDK。
安装 安装
composer require mantoufan/yzhangateway
使用 使用
{root}/src
客户端
- 在
src\Client目录下创建一个{客户端名称}.php - 在
src\Client目录新建一个{客户端名称}.php - 实现
request方法 - 实现请求
request方法
认证
如需要,新建 .php 提供鉴权类
例如包含获取 authorization 请求头的方法
如必要,创建一个新的 .php 文件以提供认证类,
例如一个用于获取 authorization 请求头的方法
异常
如需要,新建 .php 声明新错误类型
如需要,在此处创建一个新的 .php 文件以声明新的错误类型
工具
客户端工具
提供 Request 静态方法,用于发送请求和响应
提供 Request 静态方法以发送请求和响应
{root}/tests
{root}/.env.test
在运行 composer test 前,将 .env.test.template 重命名为 .env.test 以存储测试所需的变量
在运行 composer test 之前,将 .env.test.template 重命名为 env.test 以存储测试所需的变量
示例 示例
通用 通用
$yzhanGateway = new YZhanGateway('Common'); $res = $yzhanGateway->request(array( 'method' => 'GET', 'url' => 'https://animechan.vercel.app/api/random' ));
使用缓存 使用缓存
缓存结果 86400 秒
$yzhanGateway = new YZhanGateway('Common'); $res = $yzhanGateway->cache()->request(array( 'method' => 'GET', 'url' => 'https://animechan.vercel.app/api/random', 'cache' => array( 'maxAge' => 86400 ) ));
百度智能云
通过百度智能云 CDN 中的 URL 清理文件。
$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
通过 Cloudflare 清理文件(<= 30)。
$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
获取用户的最近活动。
$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
使用 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 ) ));
腾讯云
获取 CVM 列表
$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') ), ) ));