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.
32 lines
1.2 KiB
32 lines
1.2 KiB
3 months ago
|
<?php
|
||
|
|
||
|
namespace app\utility;
|
||
|
|
||
|
use GuzzleHttp\Client;
|
||
|
use GuzzleHttp\Exception\GuzzleException;
|
||
|
use think\facade\Log;
|
||
|
|
||
|
class HttpHandler
|
||
|
{
|
||
|
public static function reqPost(string $url, array $headers, array $data): array
|
||
|
{
|
||
|
try {
|
||
|
Log::info("HttpHandler - 请求URL==".$url." 请求头==".json_encode($headers)." 请求body==".json_encode($data));
|
||
|
$client = new Client();
|
||
|
$response = $client->post($url, [
|
||
|
'headers' => $headers,
|
||
|
'json' => $data,
|
||
|
]);
|
||
|
$statusCode = $response->getStatusCode();
|
||
|
$getBody = $response->getBody()->getContents();
|
||
|
Log::info("HttpHandler - 请求结果:statusCode==".$statusCode." getBody==".$getBody);
|
||
|
return ['code'=>$statusCode, 'message'=>'', 'data'=>$getBody];
|
||
|
} catch (GuzzleException $e) {
|
||
|
Log::error("HttpHandler - 请求异常 - guzzle: " . $e->getMessage());
|
||
|
return ['code'=>500, 'message'=>$e->getMessage()];
|
||
|
} catch (\Exception $exception) {
|
||
|
Log::error("HttpHandler - 请求异常 - exception: " . $exception->getMessage());
|
||
|
return ['code'=>500, 'message'=>$exception->getMessage()];
|
||
|
}
|
||
|
}
|
||
|
}
|