6 changed files with 119 additions and 6 deletions
@ -0,0 +1,32 @@ |
|||
<?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()]; |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue