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.
Use Composer to install the package using:
composer require ion-bazan/aliyun-http-signer
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.
<?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);
}
<?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');
<?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);
If you found a bug or security vulnerability, please open an issue
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.