public function setUp() { $this->context = new Context(['keys' => ['pda' => 'secret'], 'algorithm' => 'hmac-sha256', 'headers' => ['(request-target)', 'date']]); $stack = new HandlerStack(); $stack->setHandler(new MockHandler([new Response(200, ['Content-Length' => 0])])); $stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context)); $stack->push(Middleware::history($this->history)); $this->client = new Client(['handler' => $stack]); }
/** * @param array $options * @param array $curlOptions * @return Guzzle */ protected function createGuzzleClient(array $options = [], array $curlOptions = []) { $options = $options + $this->options; $curlOptions = $curlOptions + $this->curlOptions; $context = new Context(['keys' => [$this->apiKey => $this->apiSecret], 'algorithm' => 'hmac-sha256', 'headers' => ['(request-target)', 'Content-MD5', 'Date']]); $curlHandler = new CurlHandler($curlOptions); $handler = HandlerStack::create($curlHandler); $handler->push(GuzzleHttpSignatures::middlewareFromContext($context)); return new Guzzle($options + array('handler' => $handler, 'base_uri' => $this->apiEndpoint, 'headers' => array('User-Agent' => Blocktrail::SDK_USER_AGENT . '/' . Blocktrail::SDK_VERSION), 'http_errors' => false, 'connect_timeout' => 10, 'timeout' => 300.0, 'verify' => true, 'proxy' => '', 'debug' => false, 'config' => array(), 'auth' => '')); }
<?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']]);
<?php 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']]);