ion-bazan / aliyun-http-signer
PSR-7-compatible Alibaba Cloud API Gateway request signing implementation. Integrates with Guzzle ⛽️ and HttPlug 🐘.
Fund package maintenance!
IonBazan
Installs: 36 945
Dependents: 1
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- psr/http-message: ^1.0 || ^2.0
- ramsey/uuid: ^3.0 || ^4.0 || ^5.0
- symfony/polyfill-php82: ^1.22
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- guzzlehttp/guzzle: ^6.0 || ^7.0
- infection/infection: ~0.15
- laminas/laminas-diactoros: ^2.2 || ^3.0
- php-http/client-common: ^2.0
- phpunit/phpunit: ^10.5 || ^11
Suggests
- guzzlehttp/guzzle: ^6.0|^7.0, to use Guzzle Middleware
- ion-bazan/guzzle-bundle-aliyun-signer-plugin: to integrate with Guzzle Bundle
- laminas/laminas-diactoros: ^2.2 || ^3.0, to build HTTP requests
- php-http/client-common: ^2.0, to use HttPlug plugin
README
This library implements Alibaba Cloud API Gateway request signature calculation for PSR-7 compatible requests. It integrates with Guzzle and HttPlug but can be used with any PSR-7-compatible client.
Installation
Use Composer to install the package using:
composer require ion-bazan/aliyun-http-signer
Usage
Symfony integration
The easiest way to integrate the package with Symfony is using GuzzleBundleAliyunSignerPlugin with Guzzle Bundle.
To use it with HttplugBundle or any other Bundle, simply register RequestSigner
, Key
and RequestSignerPlugin
as services and inject the credentials to the Key
service.
Sign a PSR-7-compatible API request
<?php require_once 'vendor/autoload.php'; use IonBazan\AliyunSigner\Key; use IonBazan\AliyunSigner\RequestSigner; use Psr\Http\Message\RequestInterface; function signRequest(RequestInterface $request): RequestInterface { // Provide credentials $appId = '12345678'; $secret = base64_encode('secret'); // Create signer $signer = new RequestSigner(new Key($appId, $secret)); return $signer->signRequest($request); }
Sign an API request using Guzzle middleware
<?php require_once 'vendor/autoload.php'; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use IonBazan\AliyunSigner\Key; use IonBazan\AliyunSigner\RequestSigner; use IonBazan\AliyunSigner\Guzzle\RequestSignerMiddleware; // Provide credentials $appId = '12345678'; $secret = base64_encode('secret'); // Create signer and middleware $signer = new RequestSigner(new Key($appId, $secret)); $middleware = new RequestSignerMiddleware($signer); $stack = HandlerStack::create(); $stack->push($middleware); $client = new Client(['handler' => $stack]); $response = $client->get('https://example.com/api/v1/test');
Sign an API request using HttPlug plugin
<?php require_once 'vendor/autoload.php'; use Http\Client\Common\PluginClient; use Http\Discovery\HttpClientDiscovery; use IonBazan\AliyunSigner\Key; use IonBazan\AliyunSigner\RequestSigner; use IonBazan\AliyunSigner\HttPlug\RequestSignerPlugin; // Provide credentials $appId = '12345678'; $secret = base64_encode('secret'); // Create signer and plugin $signer = new RequestSigner(new Key($appId, $secret)); $plugin = new RequestSignerPlugin($signer); $pluginClient = new PluginClient( HttpClientDiscovery::find(), [$plugin] ); $pluginClient->sendRequest($request);
Bugs & issues
If you found a bug or security vulnerability, please open an issue
Contributing
Please feel free to submit Pull Requests adding new features or fixing bugs.
Please note that code must follow PSR-1, PSR-2, PSR-4 and PSR-7.