sendynl / php-sdk
Sendy API 的 PHP 客户端
v1.0.3
2024-07-11 12:31 UTC
Requires
- php: >=7.4.0
- ext-json: *
- guzzlehttp/guzzle: ~6.0|~7.0
Requires (Dev)
- mockery/mockery: ^1.5
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.7
README
PHP 客户端库,用于连接 Sendy API。此客户端允许您连接到 Sendy API 来创建发货和标签。
安装
可以通过 Composer 安装客户端
composer require sendynl/php-sdk
用法
身份验证
身份验证可以有两种方式:使用个人访问令牌和 OAuth 流程。
个人访问令牌
您可以在 门户 中管理您的个人访问令牌。当您拥有个人访问令牌时,您可以使用此库如下所示
<?php $connection = new \Sendy\Api\Connection(); $connection->setAccessToken('your-personal-access-token'); $connection->me->get();
OAuth
授权您的应用连接
$connection = new \Sendy\Api\Connection(); $connection->setOauthClient(true); $connection->setClientId('your-client-id') ->setClientSecret('your-client-secret') ->setRedirectUrl('your-callback-url'); $connection->redirectForAuthorization();
这将重定向用户到门户并授权您的集成到他们的账户。
连接到 API
$connection = new \Sendy\Api\Connection(); $connection->setClientId('your-client-id') ->setClientSecret('your-client-secret') ->setRedirectUrl('your-callback-url'); // Either an authorization code or a refresh token is required to fetch an access token $connection->setAuthorizationCode('authorization-code'); $connection->setRefreshToken('refresh-token'); // Optional if you have alreay stored the access token in your application $connection->setAccessToken('access-token'); $connection->setTokenExpires(1123581321); $connection->me->get(); // Store the access token, refresh token en the expiration timestamp in your application to prevent unnecessary // requesting new access tokens $connection->getAccessToken(); $connection->getRefreshToken(); $connection->getTokenExpires();
令牌刷新回调
您可以为连接提供一个 callable
,在令牌刷新时执行。这可以在您想要在必要时存储新令牌以重复使用时很有用。
$connection = new \Sendy\Api\Connection(); $connection->setOauthClient(true); $connection->setClientId('your-client-id') ->setClientSecret('your-client-secret') ->setRedirectUrl('your-callback-url'); ->setTokenUpdateCallback(function (\Sendy\Api\Connection $connection) { $data = [ 'access_token' => $connection->getAccessToken(), 'refresh_token' => $connection->getRefreshToken(), 'token_expires' => $connection->getTokenExpires(), ]; // Use this data to store the tokens in your application });
端点
API 文档中的端点映射到资源目录中定义的资源。请参阅 API 文档 获取详细文档。
贡献
有关详细信息,请参阅 CONTRIBUTING
安全性
如果您发现了关于安全性的错误,请通过 security@sendy.nl 发送邮件,而不是使用问题跟踪器。
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息