Skip to content

Latest commit

 

History

History
117 lines (83 loc) · 4.34 KB

README.md

File metadata and controls

117 lines (83 loc) · 4.34 KB

Alibaba Cloud API Gateway HTTP Request Signer for PHP

Latest version GitHub Workflow Status PHP version Codecov Mutation testing badge Scrutinizer Code Quality Downloads License

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.