99designs/http-signatures-guzzlehttp

使用 Guzzle 6 对 HTTP 消息进行签名和验证

2.0.1 2017-05-04 02:00 UTC

This package is not auto-updated.

Last update: 2024-09-13 12:31:37 UTC


README

Guzzle 6 对 99designs http-signatures 库的支持

Build Status

为 Guzzle 6 添加 99designs/http-signatures 支持。

旧版 Guzzle 版本

对于 Guzzle 4 和 5,请使用此存储库的 v1.x 版本。对于 Guzzle 3,请参阅 99designs/http-signatures-guzzle 存储库。

使用 Guzzle 6 进行签名

此库包括使用 Middleware 自动对 Guzzle 请求进行签名的支持。

您可以使用 GuzzleHttpSignatures::defaultHandlerFromContext 容易地创建默认的 Guzzle 处理器,并将 Middleware 添加到每个请求中进行签名。

use GuzzleHttp\Client;
use HttpSignatures\Context;
use HttpSignatures\GuzzleHttpSignatures;

require __DIR__ . "/../vendor/autoload.php";

$context = new Context([
    'keys' => ['examplekey' => 'secret-key-here'],
    'algorithm' => 'hmac-sha256',
    'headers' => ['(request-target)', 'date'],
]);

$handlerStack = GuzzleHttpSignatures::defaultHandlerFromContext($context);
$client = new Client(['handler' => $handlerStack]);

// The below will now send a signed request to: http://example.org/path?query=123
$response = $client->get("http://www.example.com/path?query=123", ['headers' => ['date' => 'today']]);

或者如果您正在创建自定义的 HandlerStack,您可以自己添加 Middleware

<?php

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use HttpSignatures\Context;
use HttpSignatures\GuzzleHttpSignatures;

require __DIR__ . "/../vendor/autoload.php";

$context = new Context([
    'keys' => ['examplekey' => 'secret-key-here'],
    'algorithm' => 'hmac-sha256',
    'headers' => ['(request-target)', 'date'],
]);

$handlerStack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context));
$stack->push(Middleware::history($this->history));
$client = new Client(['handler' => $handlerStack]);

// The below will now send a signed request to: http://example.org/path?query=123
$response = $client->get("http://www.example.com/path?query=123", ['headers' => ['date' => 'today']]);

贡献

欢迎拉取请求。