GuzzleHttp
Index
ใTipใ
Usage of the coroutine-based GuzzleHttp
client
For detailed usage, please refer to๏ผGuzzleHttp Doc
Install Dependencies
composer require hyperf/guzzle
Encapsulation Of Utility Class.
<?php
declare(strict_types=1);
namespace App\Lib\GuzzleHttp;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Guzzle\PoolHandler;
use Hyperf\Guzzle\RetryMiddleware;
use function Hyperf\Support\make;
class GuzzleFactory
{
/**
* ่ทๅๅธฆๆ่ฟๆฅๆฑ ็ๅ็จ็guzzleๅฎขๆท็ซฏ.
* @explain make ไปdiไธญ่ทๅๅไพ.
* @see https://docs.guzzlephp.org/en/stable/
* @param array $options ้้กน
* @return Client ๅฎขๆท็ซฏ
*/
public static function getCoroutineGuzzleClient(array $options = []): Client
{
[$handler, $retry, $config] = [
make(PoolHandler::class, ['option' => ['max_connections' => 50]]),
make(RetryMiddleware::class, ['retries' => 1, 'delay' => 10]),
[],
];
$stack = HandlerStack::create($handler);
$stack->push($retry->getMiddleware(), 'retry');
$config['handler'] = $options['handler'] ?? $stack;
$config = array_merge($config, $options);
return make(Client::class, ['config' => $config]);
}
}