123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- /*
- * This file is part of the overtrue/wechat.
- *
- * (c) overtrue <i@overtrue.me>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Libs\TikTok\Kernel\Traits;
- use GuzzleHttp\Client;
- use GuzzleHttp\ClientInterface;
- use GuzzleHttp\Exception\GuzzleException;
- use GuzzleHttp\HandlerStack;
- use Psr\Http\Message\ResponseInterface;
- /**
- * Trait HasHttpRequests.
- *
- * @author overtrue <i@overtrue.me>
- */
- trait HasHttpRequests {
- use ResponseCastable;
- /**
- * @var ClientInterface
- */
- protected $httpClient;
- /**
- * @var array
- */
- protected $middlewares = [];
- /**
- * @var HandlerStack
- */
- protected $handlerStack;
- /**
- * @var array
- */
- protected static $defaults = [
- 'curl' => [
- CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
- ],
- ];
- /**
- * Set guzzle default settings.
- *
- * @param array $defaults
- */
- public static function setDefaultOptions(array $defaults = []): void {
- self::$defaults = $defaults;
- }
- /**
- * Return current guzzle default settings.
- *
- * @return array
- */
- public static function getDefaultOptions(): array {
- return self::$defaults;
- }
- /**
- * Set GuzzleHttp\Client.
- *
- * @param ClientInterface $httpClient
- *
- * @return $this
- */
- public function setHttpClient(ClientInterface $httpClient) {
- $this->httpClient = $httpClient;
- return $this;
- }
- /**
- * Return GuzzleHttp\ClientInterface instance.
- *
- * @return ClientInterface
- */
- public function getHttpClient(): ClientInterface {
- if (!($this->httpClient instanceof ClientInterface)) {
- if (property_exists($this, 'app') && $this->app['http_client']) {
- $this->httpClient = $this->app['http_client'];
- } else {
- $this->httpClient = new Client(['handler' => HandlerStack::create($this->getGuzzleHandler())]);
- }
- }
- return $this->httpClient;
- }
- /**
- * Add a middleware.
- *
- * @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 {
- array_push($this->middlewares, $middleware);
- }
- return $this;
- }
- /**
- * Return all middlewares.
- *
- * @return array
- */
- public function getMiddlewares(): array {
- return $this->middlewares;
- }
- /**
- * Make a request.
- *
- * @param string $url
- * @param string $method
- * @param array $options
- *
- * @return ResponseInterface
- *
- * @throws GuzzleException
- */
- public function request(string $url, string $method = 'GET', array $options = []): ResponseInterface {
- $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;
- }
- $response = $this->getHttpClient()->request($method, $url, $options);
- $response->getBody()->rewind();
- return $response;
- }
- /**
- * @param HandlerStack $handlerStack
- *
- * @return $this
- */
- public function setHandlerStack(HandlerStack $handlerStack) {
- $this->handlerStack = $handlerStack;
- return $this;
- }
- /**
- * Build a handler stack.
- *
- * @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 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;
- }
- /**
- * Get guzzle handler.
- *
- * @return callable
- */
- protected function getGuzzleHandler(): callable {
- if (property_exists($this, 'app') && isset($this->app['guzzle_handler'])) {
- return is_string($handler = $this->app->raw('guzzle_handler'))
- ? new $handler()
- : $handler;
- }
- return \GuzzleHttp\choose_handler();
- }
- }
|