外包 / sdk
Outsourced API 的 PHP SDK
Requires
- php: ^7.2
- guzzlehttp/guzzle: ^7.2
- guzzlehttp/psr7: ^1.7
- psr/http-client: ^1.0
- psr/log: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-08-29 05:37:11 UTC
README
Outsourced 的 PHP SDK Outsourced
安装
composer require outsourced/sdk
使用
要与外包 API 进行任何通信,您必须创建一个 Outsourced 实例。我们提供 HttpOutsourced 类。默认情况下,此类使用 Guzzle 作为 http 客户端,但您可以选择其他客户端。
use OutsourcedSdk/HttpOutsourced; $api = HttpOutsourced::makeWithGuzzle([ 'host' => 'https://outsourced.yourdomain.com', 'accessKey' => 'project-access-key' ]);
单条日志
通过调用 HttpOutsourced 实例的 logSingle 方法将单条日志发送到外包服务器。
use OutsourcedSdk/HttpOutsourced; $api = HttpOutsourced::makeWithGuzzle([ 'host' => 'https://outsourced.yourdomain.com', 'accessKey' => 'project-access-key' ]); $api->logSingle('level', 'message', ['context' => 'any additional values']);
批量日志
通过调用 HttpOutsourced 实例的 logBatch 方法将日志批量发送到外包服务器。
use OutsourcedSdk/HttpOutsourced; $api = HttpOutsourced::makeWithGuzzle([ 'host' => 'https://outsourced.yourdomain.com', 'accessKey' => 'project-access-key' ]); $api->logBatch([ [ 'level' => 'info', 'message' => 'log #1' ], [ 'level' => 'error', 'message' => 'log #2', 'context' => [ 'custom' => 'custom value' ] ] ]);
所有日志的附加上下文
您可能希望在不指定此上下文的情况下将一些附加信息添加到所有日志中。您可以在创建 HttpOutsourced 实例时定义全局上下文。只需将 logging.context 索引添加到配置数组中,并添加您的值。
use OutsourcedSdk/HttpOutsourced; $api = HttpOutsourced::makeWithGuzzle([ 'host' => 'https://outsourced.yourdomain.com', 'accessKey' => 'project-access-key', 'logging' => [ 'context' => [ 'environment' => 'production' ] ] ]);
验证权限
通过调用 HttpOutsourced 实例的 verifyPermissions 方法向外包服务器发送权限验证请求。
use OutsourcedSdk/HttpOutsourced; $api = HttpOutsourced::makeWithGuzzle([ 'host' => 'https://outsourced.yourdomain.com', 'accessKey' => 'project-access-key' ]); $api->verifyPermissions('username', ['list', 'of', 'permissions']);
自定义 HttpClient
您的自定义客户端必须实现 Psr\Http\Client\ClientInterface 接口。然后您可以使用您的类的实例作为 HttpOutsourced 的 make 静态方法的第一参数。
use OutsourcedSdk/HttpOutsourced; $api = HttpOutsourced::make(new MyClient(), [ 'host' => 'https://outsourced.yourdomain.com', 'accessKey' => 'project-access-key' ]); $api->verifyPermissions('username', ['list', 'of', 'permissions']);
日志记录
每条日志由 2 个必需属性和 1 个可选属性组成
- 级别 - 应该是以下 8 个选项之一:
debug、info、notice、warning、error、critical、alert、emergency - 消息 - 记录事件的描述
- 上下文 *可选 - 附加记录的值
默认上下文
如果您通过调用 make 或 makeWithGuzzle 创建 HttpOutsourced 实例,您将自动在每条日志上下文中收到这些值
- url - 当前 URL
- method - 当前请求方法
- ip - 用户 IP
- user_agent - 用户代理名称
- server - 服务器软件
- runtime - 语言 + 版本(在这种情况下为 php + php_version)
LoggerInterface
要使用此 SDK 作为 PSR-3 logger,您可以使用我们的 LoggerInterface 实现,OutsourcedLogger。