2022-11-07 16:29:37 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Helper;
|
|
|
|
|
|
2022-11-14 14:17:01 +08:00
|
|
|
|
use App\Exception\BusinessException;
|
2022-11-07 16:29:37 +08:00
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
|
use Hyperf\Guzzle\HandlerStackFactory;
|
|
|
|
|
|
2022-11-08 11:38:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 请求处理公共类
|
|
|
|
|
*/
|
2022-11-07 16:29:37 +08:00
|
|
|
|
class Curl
|
|
|
|
|
{
|
2022-11-08 11:38:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取Guzzle客户端实例(使用连接池)
|
|
|
|
|
*
|
|
|
|
|
* @return Client
|
|
|
|
|
*/
|
|
|
|
|
protected static function getClient() : Client
|
2022-11-07 16:29:37 +08:00
|
|
|
|
{
|
|
|
|
|
$factory = new HandlerStackFactory();
|
|
|
|
|
$stack = $factory->create();
|
|
|
|
|
|
2022-11-08 11:38:31 +08:00
|
|
|
|
return make(Client::class, [
|
2022-11-07 16:29:37 +08:00
|
|
|
|
'config' => [
|
|
|
|
|
'handler' => $stack,
|
|
|
|
|
]
|
|
|
|
|
]);
|
2022-11-08 11:38:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送Get请求
|
|
|
|
|
*
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param array $headers
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get(string $url, array $headers = []) : array
|
|
|
|
|
{
|
|
|
|
|
$client = self::getClient();
|
|
|
|
|
$options = ['timeout' => 2, 'verify' => false];
|
|
|
|
|
|
|
|
|
|
if (!empty($headers)) {
|
|
|
|
|
$options['headers'] = $headers;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 14:17:01 +08:00
|
|
|
|
try {
|
|
|
|
|
$response = $client->get($url, $options);
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
throw new BusinessException($throwable->getCode(), $throwable->getMessage());
|
|
|
|
|
}
|
2022-11-07 16:29:37 +08:00
|
|
|
|
|
|
|
|
|
if ($response->getStatusCode() === 200) {
|
|
|
|
|
return json_decode($response->getBody()->getContents(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 11:38:31 +08:00
|
|
|
|
/**
|
|
|
|
|
* 发送POST请求(application/x-www-form-urlencoded)
|
|
|
|
|
*
|
|
|
|
|
* @param string $url
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @param array $headers
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function post(string $url, array $data = [], array $headers = []) : array
|
2022-11-07 16:29:37 +08:00
|
|
|
|
{
|
2022-11-08 11:38:31 +08:00
|
|
|
|
$client = self::getClient();
|
|
|
|
|
$options = ['timeout' => 2, 'verify' => false, 'form_params' => $data];
|
|
|
|
|
|
|
|
|
|
if (!empty($headers)) {
|
|
|
|
|
$options['headers'] = $headers;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 14:17:01 +08:00
|
|
|
|
try {
|
|
|
|
|
$response = $client->post($url, $options);
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
throw new BusinessException($throwable->getCode(), $throwable->getMessage());
|
|
|
|
|
}
|
2022-11-08 11:38:31 +08:00
|
|
|
|
|
|
|
|
|
if ($response->getStatusCode() === 200) {
|
|
|
|
|
return json_decode($response->getBody()->getContents(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
2022-11-07 16:29:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|