feat: 增加 Guzzle 客户端单例复用逻辑,优化 API 请求性能
This commit is contained in:
parent
9f0b4cfeeb
commit
7604a5f491
|
@ -15,8 +15,31 @@ class Rocketgo
|
||||||
/**
|
/**
|
||||||
* TOKEN
|
* TOKEN
|
||||||
*/
|
*/
|
||||||
protected static $token="eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjFlMTRlZWQ3LTEyODUtNGFjNi1hMzFhLTFlNWQ5OTEzMTJhYSJ9.BJgOhw1VKX1i9VfYKLIzF1zXUZMi4idO9Sb-p6p_rMKJanmt4is9slky7SqvSEXqhJ6USMGs2wqMY3tYJrH-hw";
|
protected static $token = "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjFlMTRlZWQ3LTEyODUtNGFjNi1hMzFhLTFlNWQ5OTEzMTJhYSJ9.BJgOhw1VKX1i9VfYKLIzF1zXUZMi4idO9Sb-p6p_rMKJanmt4is9slky7SqvSEXqhJ6USMGs2wqMY3tYJrH-hw";
|
||||||
protected static $baseurl="https://dx1.rocketgo.vip";
|
protected static $baseurl = "https://dx1.rocketgo.vip";
|
||||||
|
//单例复用http客户端
|
||||||
|
public static $client = null;
|
||||||
|
/**
|
||||||
|
* 获取 Guzzle 客户端
|
||||||
|
* 用于复用 客户端常驻内存
|
||||||
|
*
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public static function get_client()
|
||||||
|
{
|
||||||
|
if (self::$client == null) {
|
||||||
|
self::$client = new Client([
|
||||||
|
'curl' => [
|
||||||
|
CURLOPT_FRESH_CONNECT => false,
|
||||||
|
CURLOPT_FORBID_REUSE => false,
|
||||||
|
],
|
||||||
|
'headers' => [
|
||||||
|
'Connection' => 'keep-alive',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return self::$client;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 获取base64图形验证码
|
* 获取base64图形验证码
|
||||||
*
|
*
|
||||||
|
@ -27,7 +50,7 @@ class Rocketgo
|
||||||
$client = new Client(); // 创建 Guzzle 客户端
|
$client = new Client(); // 创建 Guzzle 客户端
|
||||||
// 请求数据
|
// 请求数据
|
||||||
// 发送 POST 请求
|
// 发送 POST 请求
|
||||||
$response = $client->get("self::1baseurl/prod-api1/captchaImage");
|
$response = $client->get("self::baseurl/prod-api1/captchaImage");
|
||||||
|
|
||||||
// 获取响应体内容
|
// 获取响应体内容
|
||||||
$body = $response->getBody();
|
$body = $response->getBody();
|
||||||
|
@ -41,7 +64,7 @@ class Rocketgo
|
||||||
// "code": "23",
|
// "code": "23",
|
||||||
// "uuid": "9cbe6a9037dc47cf80c9f2a23c0672a5"
|
// "uuid": "9cbe6a9037dc47cf80c9f2a23c0672a5"
|
||||||
// }
|
// }
|
||||||
public static function login($username,$password,$code,$uuid)
|
public static function login($username, $password, $code, $uuid)
|
||||||
{
|
{
|
||||||
$client = new Client(); // 创建 Guzzle 客户端
|
$client = new Client(); // 创建 Guzzle 客户端
|
||||||
// 请求数据
|
// 请求数据
|
||||||
|
|
Loading…
Reference in New Issue