custom_handler_stack_example.php 839 B

1234567891011121314151617181920212223242526
  1. <?php
  2. use GuzzleHttp\Client;
  3. use GuzzleHttp\Handler\CurlHandler;
  4. use GuzzleHttp\HandlerStack;
  5. use GuzzleHttp\Middleware;
  6. use HttpSignatures\Context;
  7. use HttpSignatures\GuzzleHttpSignatures;
  8. require __DIR__ . "/../vendor/autoload.php";
  9. $context = new Context([
  10. 'keys' => ['examplekey' => 'secret-key-here'],
  11. 'algorithm' => 'hmac-sha256',
  12. 'headers' => ['(request-target)', 'date'],
  13. ]);
  14. $handlerStack = new HandlerStack();
  15. $stack->setHandler(new CurlHandler());
  16. $stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context));
  17. $stack->push(Middleware::history($this->history));
  18. $client = new Client(['handler' => $handlerStack]);
  19. // The below will now send a signed request to: http://example.org/path?query=123
  20. $response = $client->get("http://www.example.com/path?query=123", ['headers' => ['date' => 'today']]);