2025-05-12 00:19:35 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\BaseApp;
|
|
|
|
|
|
|
2025-12-01 10:25:19 +08:00
|
|
|
|
use Exception;
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
|
|
|
2025-05-12 00:19:35 +08:00
|
|
|
|
class BaseClient
|
|
|
|
|
|
{
|
|
|
|
|
|
private static mixed $_instance;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取实例
|
|
|
|
|
|
* @return null|static
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static function getInstance(): null|static
|
|
|
|
|
|
{
|
|
|
|
|
|
$name = get_called_class();
|
|
|
|
|
|
if (!isset(self::$_instance[$name])) {
|
|
|
|
|
|
self::$_instance[$name] = new static();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return self::$_instance[$name];
|
|
|
|
|
|
}
|
2025-12-01 10:25:19 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 初始化请求类
|
|
|
|
|
|
* @return $this
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function init(): static
|
|
|
|
|
|
{
|
|
|
|
|
|
if (empty($this->baseUrl)) {
|
|
|
|
|
|
$this->utils->errorThrow('请设置baseUrl');
|
|
|
|
|
|
}
|
|
|
|
|
|
$this->client = new Client([
|
|
|
|
|
|
'timeout' => 60.0,
|
|
|
|
|
|
'connect_timeout' => 10.0,
|
|
|
|
|
|
'base_uri' => $this->baseUrl,
|
|
|
|
|
|
'verify' => false, // 禁用SSL证书验证(不推荐用于生产环境)
|
|
|
|
|
|
]);
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* GET 请求
|
|
|
|
|
|
* @param $uri
|
|
|
|
|
|
* @param array $options
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function get($uri, array $options = []): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->init()->request('GET', $uri, [ 'query' => $options, 'headers' => $this->headers ]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* POST 请求
|
|
|
|
|
|
* @param $uri
|
|
|
|
|
|
* @param array $options
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function post($uri, array $options = []): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->init()->request('POST', $uri, [ 'form_params' => $options, 'headers' => $this->headers ]);
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* POST 请求
|
|
|
|
|
|
* @param $uri
|
|
|
|
|
|
* @param array $options
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function postJson($uri, array $options = []): array
|
|
|
|
|
|
{
|
|
|
|
|
|
// ds([ 'json' => $options, 'headers' => $this->headers, 'url' => $this->baseUrl. $uri ]);
|
|
|
|
|
|
return $this->init()->request('POST', $uri, [ 'json' => $options, 'headers' => $this->headers ]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 请求
|
|
|
|
|
|
* @param $method
|
|
|
|
|
|
* @param $uri
|
|
|
|
|
|
* @param array $options
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
private function request($method, $uri, array $options): array
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$response = $this->client->request($method, $uri, $options);
|
|
|
|
|
|
return [
|
|
|
|
|
|
'status_code' => $response->getStatusCode(),
|
|
|
|
|
|
'body' => json_decode((string)$response->getBody(), true),
|
|
|
|
|
|
];
|
|
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
|
|
return [
|
|
|
|
|
|
'status_code' => $e->getCode(),
|
|
|
|
|
|
'error_message' => $e->getMessage(),
|
|
|
|
|
|
'type' => 'RequestException',
|
|
|
|
|
|
'body' => []
|
|
|
|
|
|
];
|
|
|
|
|
|
} catch (GuzzleException $e) {
|
|
|
|
|
|
return [
|
|
|
|
|
|
'status_code' => $e->getCode(),
|
|
|
|
|
|
'error_message' => $e->getMessage(),
|
|
|
|
|
|
'type' => 'GuzzleException',
|
|
|
|
|
|
'body' => []
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-12 00:19:35 +08:00
|
|
|
|
}
|