本文整理汇总了PHP中GuzzleHttp\HandlerStack::setHandler方法的典型用法代码示例。如果您正苦于以下问题:PHP HandlerStack::setHandler方法的具体用法?PHP HandlerStack::setHandler怎么用?PHP HandlerStack::setHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\HandlerStack
的用法示例。
在下文中一共展示了HandlerStack::setHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* Create the Stack and set a default handler
*/
public function __construct()
{
if (!self::$stack) {
self::$stack = HandlerStack::create();
self::$stack->setHandler(\GuzzleHttp\choose_handler());
}
}
示例2: __construct
public function __construct()
{
static::init();
$Stack = new HandlerStack();
$Stack->setHandler(new CurlHandler());
/**
* Здесь ставим ловушку, чтобы с помощью редиректов
* определить адрес сервера, который сможет отсылать сообщения
*/
$Stack->push(Middleware::mapResponse(function (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(Middleware::mapResponse(function (ResponseInterface $Response) {
$header = $Response->getHeader("Set-RegistrationToken");
if (count($header) > 0) {
$this->regToken = trim(explode(';', $header[0])[0]);
}
return $Response;
}));
//$cookieJar = new FileCookieJar('cookie.txt', true);
$this->client = new Client(['handler' => $Stack, 'cookies' => true]);
}
示例3: setUp
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]);
}
示例4: testExceptionWhileNewIdentity
/**
* @expectedException \GuzzleTor\TorNewIdentityException
*/
public function testExceptionWhileNewIdentity()
{
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::tor('127.0.0.1:9050', 'not-existed-host:9051'));
$client = new Client(['handler' => $stack]);
// Throw TorNewIdentityException because of wrong tor control host
$client->get('https://check.torproject.org/', ['tor_new_identity' => true, 'tor_new_identity_exception' => true]);
}
示例5: __construct
public function __construct($bucket, $pub_key, $sec_key, $suffix = '.ufile.ucloud.cn', $https = false, $debug = false)
{
$this->bucket = $bucket;
$this->pub_key = $pub_key;
$this->sec_key = $sec_key;
$this->host = ($https ? 'https://' : 'http://') . $bucket . $suffix;
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(static::auth($bucket, $pub_key, $sec_key));
$this->httpClient = new Client(['base_uri' => $this->host, 'handler' => $stack, 'debug' => $debug]);
}
示例6: __construct
/**
* HttpClient constructor.
*
* @param string $key
* @param string $secret
* @param array $options
*/
public function __construct($key, $secret, array $options = [])
{
$options = array_merge($this->options, $options);
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use($key, $secret, $options) {
$date = new \DateTime('now', new \DateTimeZone('UTC'));
return $request->withHeader('User-Agent', $options['user_agent'])->withHeader('Apikey', $key)->withHeader('Date', $date->format('Y-m-d H:i:s'))->withHeader('Signature', sha1($secret . $date->format('Y-m-d H:i:s')));
}));
$this->options = array_merge($this->options, $options, ['handler' => $stack]);
$this->options['base_uri'] = sprintf($this->options['base_uri'], $this->options['api_version']);
$this->client = new Client($this->options);
}
示例7: get_tor_ip
function get_tor_ip()
{
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::tor());
$client = new Client(['handler' => $stack]);
$response = $client->get('https://check.torproject.org/');
if (preg_match('/<strong>([\\d.]+)<\\/strong>/', $response->getBody(), $matches)) {
return $matches[1];
} else {
return null;
}
}
示例8: testRegisterPlugin
public function testRegisterPlugin()
{
$middleware = $this->getMiddleware();
$container = [];
$history = Middleware::history($container);
$stack = new HandlerStack();
$stack->setHandler(new MockHandler([new Response(200)]));
$stack->push($middleware);
$stack->push($history);
$client = new Client(['base_url' => 'http://example.com', 'handler' => $stack]);
$client->get('/resource/1');
$transaction = reset($container);
$request = $transaction['request'];
$authorization = $request->getHeaderLine('Authorization');
$this->assertRegExp('@Acquia 1:([a-zA-Z0-9+/]+={0,2})$@', $authorization);
}
示例9: register
private function register()
{
$this->container->add('WpsClient', function () {
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
//print_r((string)$request->getUri() . PHP_EOL);
return $request;
}));
return WpsClient::factory(['handler' => $stack, 'base_uri' => $this->config['base_uri'], 'auth' => $this->config['auth'], 'headers' => ['Content-Type' => 'application/json']]);
});
$this->container->add('RequestBuilder', function ($args) {
$client = $this->container->get('WpsClient');
$uriGenerator = new UriGenerator();
return new RequestBuilder($client, $uriGenerator, $args);
});
}
示例10: __construct
/**
* @param string $apiKey
* @param string $secret
* @param string $url
* @param ClientInterface|null $client
* @param BucketManager $bucketManager
* @param FileManager $fileManager
* @param array $connectionConfig
*/
public function __construct($apiKey, $secret, $url = 'https://myracloud-upload.local/v2/', ClientInterface $client = null, BucketManager $bucketManager = null, FileManager $fileManager = null, array $connectionConfig = [])
{
$this->apiKey = $apiKey;
$this->secret = $secret;
$this->url = $url;
if ($client === null) {
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::prepareBody());
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use($secret, $apiKey) {
return $request->withHeader('Authorization', "MYRA {$apiKey}:" . new Authentication\Signature($request->getMethod(), $request->getRequestTarget(), $secret, $request->getHeaders(), $request->getBody()));
}));
$client = new Client(array_merge(['base_uri' => $this->url, 'handler' => $stack], $connectionConfig));
}
$this->client = $client;
$this->bucketManager = $bucketManager ?: new BucketManager($this->client);
$this->fileManager = $fileManager ?: new FileManager($this->client);
}
示例11: makeStack
public function makeStack(CurlHandler $curl, HandlerStack $stack)
{
$stack->setHandler($curl);
$stack->push(function (callable $handler) {
return function (RequestInterface $request, array $options) use($handler) {
$method = $request->getMethod();
$uri = '/' . trim($request->getUri()->getPath(), '/');
$qs = $request->getUri()->getQuery();
if ($qs) {
$qs = '?' . $qs;
}
$header = $this->getAuthenticationHeader($method, $uri);
$request = $request->withHeader('Authentication', $header);
$request = $request->withUri(Psr7\uri_for(static::BASE_URL . $uri . $qs));
return $handler($request, $options);
};
});
return $stack;
}
示例12: testIntegrationResponseHandler
public function testIntegrationResponseHandler()
{
$options = $this->getOptions();
$options->setDatabase("mydb");
$client = $this->getClient();
$client->createDatabase("mydb");
$client->mark(["time" => "2015-09-10T23:20:35Z", "points" => [["measurement" => "cpu", "fields" => ["value" => "OK", "hello" => 2]], ["measurement" => "mem", "fields" => ["value" => "KO", "hello" => 4]]]]);
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(\InfluxDB\Handler\message_handler());
// Push the response handler
$http = new HttpClient(['handler' => $stack]);
$options = new Http\Options();
$options->setDatabase("mydb");
$reader = new Http\Reader($http, $options);
$writer = new Http\Writer($http, $options);
$client = new Client($reader, $writer);
$response = $client->query("SELECT * FROM cpu,mem");
$this->assertEquals(["cpu" => [["value" => "OK", "hello" => 2, "time" => "2015-09-10T23:20:35Z"]], "mem" => [["value" => "KO", "hello" => 4, "time" => "2015-09-10T23:20:35Z"]]], $response);
}
示例13: buildGuzzleStack
/**
* @return HandlerStack
*/
protected function buildGuzzleStack()
{
// New stack
$stack = new HandlerStack();
// Set handler for the stack, let Guzzle choose
$stack->setHandler(\GuzzleHttp\choose_handler());
// Add Request middleware to the stack that logs the url
$stack->push(Middleware::mapRequest($this->buildMiddlewareLogRequestUrl()));
// Return
return $stack;
}
示例14: buildHandlerStack
/**
* Creates a HandlerStack based off of the supplied configuration.
*
* @param string $handlerClass full class name to use for the HTTP handler implementation
* @return HandlerStack configured HandlerStack implementation
*/
protected function buildHandlerStack($handlerClass)
{
$stack = new HandlerStack();
$stack->setHandler(new $handlerClass());
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
$timestamp = gmdate('r');
$authorization = $this->getAuthorization($timestamp);
$requestInterface = $request->withHeader('Date', $timestamp)->withHeader('x-request-id', (string) Uuid::uuid4());
if (!$this->noAuth) {
$requestInterface = $requestInterface->withHeader('Authorization', $authorization);
}
return $requestInterface;
}));
$decider = function ($retries, Request $request, Response $response = null, RequestException $exception = null) {
// Stop retrying if we have exceeded our max
if ($retries > $this->retries) {
return false;
}
// Retry connection exceptions
if ($exception instanceof ConnectException) {
return true;
}
if ($response) {
// Retry on server errors
if ($response->getStatusCode() >= 500) {
return true;
}
}
return false;
};
if ($this->retries > 0) {
$stack->push(Middleware::retry($decider));
}
return $stack;
}
示例15: getHandler
/**
* Build a handler.
*
* @return HandlerStack
*/
protected function getHandler()
{
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
foreach ($this->middlewares as $middleware) {
$stack->push($middleware);
}
return $stack;
}