Пример #1
0
 public function __construct()
 {
     static::init();
     $Stack = new \GuzzleHttp\HandlerStack();
     $Stack->setHandler(new \GuzzleHttp\Handler\CurlHandler());
     /**
      * Здесь ставим ловушку, чтобы с помощью редиректов
      *   определить адрес сервера, который сможет отсылать сообщения
      */
     $Stack->push(\GuzzleHttp\Middleware::mapResponse(function (\Psr\Http\Message\ResponseInterface $Response) {
         $code = $Response->getStatusCode();
         if ($code >= 301 && $code <= 303 || $code == 307 || $code == 308) {
             $location = $Response->getHeader('Location');
             preg_match('/https?://([^-]*-)client-s/', $location, $matches);
             if (array_key_exists(1, $matches)) {
                 $this->cloud = $matches[1];
             }
         }
         return $Response;
     }));
     /**
      * Ловушка для отлова хедера Set-RegistrationToken
      * Тоже нужен для отправки сообщений
      */
     $Stack->push(\GuzzleHttp\Middleware::mapResponse(function (\Psr\Http\Message\ResponseInterface $Response) {
         $header = $Response->getHeader("Set-RegistrationToken");
         if (count($header) > 0) {
             $this->regToken = trim(explode(';', $header[0])[0]);
         }
         return $Response;
     }));
     $this->Client = new Client(['handler' => $Stack, 'cookies' => true]);
 }
Пример #2
0
 protected function buildGuzzleFromResponses(array $responses, $history = null)
 {
     $mock = new \GuzzleHttp\Handler\MockHandler($responses);
     $handler = new \GuzzleHttp\HandlerStack($mock);
     if ($history) {
         $handler->push($history);
     }
     return new HttpClient(['handler' => $handler]);
 }
 private function __getClient()
 {
     $config = [];
     if (isset($this->_config['sparkpost']['log'])) {
         $stack = GuzzleHttp\HandlerStack::create();
         $stack->push(GuzzleHttp\Middleware::log(class_exists('\\Cake\\Log\\Log') ? new \Cake\Log\Log() : new Cake2PsrLog(), new GuzzleHttp\MessageFormatter(isset($this->_config['sparkpost']['log']['format']) ? $this->_config['sparkpost']['log']['format'] : '{response}'), isset($this->_config['sparkpost']['log']['level']) ? $this->_config['sparkpost']['log']['level'] : 'debug'));
         $config = ['handler' => $stack];
     }
     return new GuzzleHttp\Client($config);
 }
Пример #4
0
<?php

require '../vendor/autoload.php';
// Get URI object.
$uri = Zend\Diactoros\ServerRequestFactory::fromGlobals()->getUri();
// Setup DebugBar
$debugbar = new DebugBar\StandardDebugBar();
$debugbar->getJavascriptRenderer()->setBaseUrl($uri->withPort('8000')->withPath('/'));
// Setting up the stack off middlewares.
$handler = GuzzleHttp\HandlerStack::create();
$middleware = new GuzzleHttp\Profiling\Middleware(new GuzzleHttp\Profiling\Debugbar\Profiler($debugbar->getCollector('time')));
$handler->unshift($middleware);
$handler->unshift(Demo1\GithubAuth::create());
// Boot application.
$controller = new Demo6\Controller(new GuzzleHttp\Client(compact('handler')), $debugbar->getJavascriptRenderer());
// Let application do the magic.
$response = $controller->index();
// Output Response.
(new Zend\Diactoros\Response\SapiEmitter())->emit($response);
 /**
  * Return Guzzle Mock
  * 
  * @param int $secondStatus
  * @param array $secondHeaders
  * @param string $secondBody
  * @return GuzzleHttp\Client
  */
 private function getGuzzleMock($secondStatus = null, $secondHeaders = [], $secondBody = null)
 {
     $mock = new GuzzleHttp\Handler\MockHandler([new GuzzleHttp\Psr7\Response(200, ['Content-Type' => [0 => 'application/json; charset=utf-8']], '{"token_type":"http://schemas.xmlsoap.org/ws/2009/11/swt-token-profile-1.0","access_token":"lorem-ipsum-access-token","expires_in":"599","scope":"lorem ipsum scope"}'), new GuzzleHttp\Psr7\Response($secondStatus, $secondHeaders, $secondBody)]);
     $handler = GuzzleHttp\HandlerStack::create($mock);
     return new GuzzleHttp\Client(['handler' => $handler]);
 }