hospital-applets-api/app/Utils/HttpRequest.php
wucongxing8150 bde4ef5a0f
Some checks failed
Build Docker / build (push) Has been cancelled
新增了字段返回
2025-09-04 13:01:00 +08:00

58 lines
1.4 KiB
PHP

<?php
namespace App\Utils;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Di\Annotation\Inject;
/**
* guzzle请求封装
*/
class HttpRequest
{
#[Inject]
protected Client $client;
/**
* 请求封装
* post请求
* @param string $path
* @param array $option
* @return array
* @throws GuzzleException
*/
public function postRequest(string $path, array $option = []): array
{
$response = $this->client->post($path, $option);
if ($response->getStatusCode() != '200'){
// 请求失败
throw new BusinessException(HttpEnumCode::SERVER_ERROR,$response->getBody()->getContents());
}
return json_decode($response->getBody(),true);
}
/**
* 请求封装
* post请求
* @param string $path
* @param array $option
* @return array
* @throws GuzzleException
*/
public function getRequest(string $path, array $option = []): array
{
$response = $this->client->get($path, $option);
if ($response->getStatusCode() != '200'){
// 请求失败
throw new BusinessException(HttpEnumCode::SERVER_ERROR,$response->getBody()->getContents());
}
return json_decode($response->getBody(),true);
}
}