本文整理汇总了PHP中GuzzleHttp\Middleware::cookies方法的典型用法代码示例。如果您正苦于以下问题:PHP Middleware::cookies方法的具体用法?PHP Middleware::cookies怎么用?PHP Middleware::cookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Middleware
的用法示例。
在下文中一共展示了Middleware::cookies方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* 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;
}
示例2: testAddsCookiesToRequests
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: initClient
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: __construct
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]]);
}