示例#1
0
 /**
  * Creates a default handler stack that can be used by clients.
  *
  * The returned handler will wrap the provided handler or use the most
  * appropriate default handler for you system. The returned HandlerStack has
  * support for cookies, redirects, HTTP error exceptions, and preparing a body
  * before sending.
  *
  * The returned handler stack can be passed to a client in the "handler"
  * option.
  *
  * @param callable $handler HTTP handler function to use with the stack. If no
  *                          handler is provided, the best handler for your
  *                          system will be utilized.
  *
  * @return HandlerStack
  */
 public static function create(callable $handler = null)
 {
     $stack = new self($handler ?: choose_handler());
     $stack->push(Middleware::httpErrors(), 'http_errors');
     $stack->push(Middleware::redirect(), 'allow_redirects');
     $stack->push(Middleware::cookies(), 'cookies');
     $stack->push(Middleware::prepareBody(), 'prepare_body');
     return $stack;
 }
 public function testAddsCookiesToRequests()
 {
     $jar = new CookieJar();
     $m = Middleware::cookies($jar);
     $h = new MockHandler([function (RequestInterface $request) {
         return new Response(200, ['Set-Cookie' => new SetCookie(['Name' => 'name', 'Value' => 'value', 'Domain' => 'foo.com'])]);
     }]);
     $f = $m($h);
     $f(new Request('GET', 'http://foo.com'), ['cookies' => $jar])->wait();
     $this->assertCount(1, $jar);
 }
示例#3
0
 protected function initClient()
 {
     $handlerStack = HandlerStack::create();
     /** @noinspection PhpUnusedParameterInspection */
     $handlerStack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, $error = null) {
         return $response && $response->getStatusCode() == 503;
     }, function ($retries) {
         return rand(60, 600) * 1000;
     }));
     /** @noinspection PhpUnusedParameterInspection */
     $handlerStack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, $error = null) {
         if ($retries >= self::RETRIES_COUNT) {
             return false;
         } elseif ($response && $response->getStatusCode() > 499) {
             return true;
         } elseif ($error) {
             return true;
         } else {
             return false;
         }
     }, function ($retries) {
         return (int) pow(2, $retries - 1) * 1000;
     }));
     $handlerStack->push(Middleware::cookies());
     if ($this->logger) {
         $handlerStack->push(Middleware::log($this->logger, $this->loggerFormatter));
     }
     $this->guzzle = new \GuzzleHttp\Client(array_merge(['handler' => $handlerStack, 'cookies' => true], $this->guzzleOptions));
 }
示例#4
-1
 public function __construct($username, $password, $url = '')
 {
     $this->username = $username;
     $this->password = $password;
     if ($url && substr($url, -1) != '/') {
         $url .= '/';
     }
     $this->url = $url ?: self::URL;
     $handlerStack = HandlerStack::create();
     // Retry from maintenance, makes 5-10 hours of waiting
     /** @noinspection PhpUnusedParameterInspection */
     $handlerStack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, $error = null) {
         return $retries < self::MAINTENANCE_RETRIES_COUNT && $response && ($response->getStatusCode() == 503 || $response->getStatusCode() == 423);
     }, function ($retries) {
         return rand(60, 600) * 1000;
     }));
     // Retry for server errors
     /** @noinspection PhpUnusedParameterInspection */
     $handlerStack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, $error = null) {
         if ($retries >= self::RETRIES_COUNT) {
             return false;
         } elseif ($response && $response->getStatusCode() > 499) {
             return true;
         } elseif ($error) {
             return true;
         } else {
             return false;
         }
     }, function ($retries) {
         return (int) pow(2, $retries - 1) * 1000;
     }));
     $handlerStack->push(Middleware::cookies());
     /*if ($this->logger) {
           $handlerStack->push(Middleware::log($this->logger, $this->loggerFormatter));
       }*/
     $this->client = new \GuzzleHttp\Client(['base_uri' => $this->url, 'handler' => $handlerStack, 'auth' => [$username, $password]]);
 }