mirror of
https://gitee.com/niucloud-team/niucloud-admin.git
synced 2026-05-15 13:23:43 +00:00
184 lines
4.3 KiB
PHP
184 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace core\util\niucloud\http;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\ClientInterface;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use GuzzleHttp\HandlerStack;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use function GuzzleHttp\choose_handler;
|
|
|
|
|
|
trait HasHttpRequests
|
|
{
|
|
/**
|
|
* curl的自定义选项
|
|
* @var array
|
|
*/
|
|
protected static array $defaults = [
|
|
'curl' => [
|
|
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
|
|
],
|
|
];
|
|
/**
|
|
* @var ClientInterface
|
|
*/
|
|
protected $httpClient;
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected array $middlewares = [];
|
|
/**
|
|
* @var HandlerStack
|
|
*/
|
|
protected $handlerStack;
|
|
|
|
/**
|
|
* @param array $defaults
|
|
* @return void
|
|
*/
|
|
public static function setDefaultOptions(array $defaults = [])
|
|
{
|
|
self::$defaults = $defaults;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public static function getDefaultOptions(): array
|
|
{
|
|
return self::$defaults;
|
|
}
|
|
|
|
/**
|
|
* @param callable $middleware
|
|
* @param string|null $name
|
|
* @return $this
|
|
*/
|
|
public function pushMiddleware(callable $middleware, string $name = null)
|
|
{
|
|
if (!is_null($name)) {
|
|
$this->middlewares[$name] = $middleware;
|
|
} else {
|
|
$this->middlewares[] = $middleware;
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getMiddlewares(): array
|
|
{
|
|
return $this->middlewares;
|
|
}
|
|
|
|
/**
|
|
* @param $url
|
|
* @param string $method
|
|
* @param array $options
|
|
* @return ResponseInterface
|
|
* @throws GuzzleException
|
|
*/
|
|
public function toRequest($url, string $method = 'GET', array $options = [])
|
|
{
|
|
$method = strtoupper($method);
|
|
|
|
$options = array_merge(self::$defaults, $options, ['handler' => $this->getHandlerStack()]);
|
|
|
|
$options = $this->fixJsonIssue($options);
|
|
|
|
if (property_exists($this, 'baseUri') && !is_null($this->baseUri)) {
|
|
$options['base_uri'] = $this->baseUri;
|
|
}
|
|
$options['connect_timeout'] = config('niucloud.http.connect_timeout', 3);
|
|
$response = $this->getHttpClient()->request($method, $url, $options);
|
|
$response->getBody()->rewind();
|
|
return json_decode($response->getBody()->getContents(), true);
|
|
}
|
|
|
|
/**
|
|
* @return HandlerStack
|
|
*/
|
|
public function getHandlerStack(): HandlerStack
|
|
{
|
|
if ($this->handlerStack) {
|
|
return $this->handlerStack;
|
|
}
|
|
|
|
$this->handlerStack = HandlerStack::create($this->getGuzzleHandler());
|
|
|
|
foreach ($this->middlewares as $name => $middleware) {
|
|
$this->handlerStack->push($middleware, $name);
|
|
}
|
|
|
|
return $this->handlerStack;
|
|
}
|
|
|
|
/**
|
|
* @param HandlerStack $handlerStack
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function setHandlerStack(HandlerStack $handlerStack)
|
|
{
|
|
$this->handlerStack = $handlerStack;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return callable
|
|
*/
|
|
protected function getGuzzleHandler()
|
|
{
|
|
return choose_handler();
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
* @return array
|
|
*/
|
|
protected function fixJsonIssue(array $options): array
|
|
{
|
|
if (isset($options['json']) && is_array($options['json'])) {
|
|
$options['headers'] = array_merge($options['headers'] ?? [], ['Content-Type' => 'application/json']);
|
|
|
|
if (empty($options['json'])) {
|
|
$options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_FORCE_OBJECT);
|
|
} else {
|
|
$options['body'] = \GuzzleHttp\json_encode($options['json'], JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
unset($options['json']);
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* @return ClientInterface
|
|
*/
|
|
public function getHttpClient(): ClientInterface
|
|
{
|
|
if (!($this->httpClient instanceof ClientInterface)) {
|
|
$this->httpClient = new Client(['handler' => HandlerStack::create($this->getGuzzleHandler())]);
|
|
}
|
|
|
|
return $this->httpClient;
|
|
}
|
|
|
|
/**
|
|
* @param ClientInterface $httpClient
|
|
* @return $this
|
|
*/
|
|
public function setHttpClient(ClientInterface $httpClient)
|
|
{
|
|
$this->httpClient = $httpClient;
|
|
|
|
return $this;
|
|
}
|
|
}
|