p2 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.6 KiB

5 months ago
<?php
namespace app\utility;
4 months ago
use app\home\service\BaseHomeService;
5 months ago
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use think\facade\Log;
4 months ago
class RequestChatServer extends BaseHomeService
5 months ago
{
4 months ago
public function ReqChatServer(string $url, array $data, string $method = 'POST'): array
5 months ago
{
try {
4 months ago
if (!in_array($method, ['POST','PUT'])) {
return $this->toData('500', '请求方法不在支持列表中');
}
2 months ago
Log::info("Guzzle请求: URL==".$url." 请求Param==".json_encode($data));
5 months ago
$resBody = [];
$client = new Client();
4 months ago
if ($method == 'PUT') {
$response = $client->put($url, [
'json' => $data,
]);
} else {
$response = $client->post($url, [
'json' => $data,
]);
}
5 months ago
$statusCode = $response->getStatusCode();
2 months ago
Log::error("Guzzle请求响应:code==".$statusCode." body==". $response->getBody());
5 months ago
if ($statusCode == 200) {
$resBody = json_decode($response->getBody(), true); // 转换为数组
}
return $resBody;
} catch (GuzzleException $e) {
2 months ago
Log::error("Guzzle请求异常 - guzzle: " . $e->getMessage());
4 months ago
return $this->toData('500', $e->getMessage());
5 months ago
} catch (\Exception $exception) {
2 months ago
Log::error("Guzzle请求异常 - exception: " . $exception->getMessage());
4 months ago
return $this->toData('500', $exception->getMessage());
5 months ago
}
}
}