|
|
@@ -4,6 +4,9 @@ use app\common\logic\TableDataLogic;
|
|
|
use app\common\model\goods_category\GoodsCategory;
|
|
|
use app\common\model\setting\PostageRegion;
|
|
|
use app\common\service\FileService;
|
|
|
+use GuzzleHttp\Client;
|
|
|
+use GuzzleHttp\Exception\GuzzleException;
|
|
|
+use think\facade\Log;
|
|
|
use think\helper\Str;
|
|
|
|
|
|
/**
|
|
|
@@ -764,4 +767,32 @@ function formatLogData($data){
|
|
|
if(is_object($data)) return json_encode((array)$data);
|
|
|
if(is_bool($data)) return $data?'true':'false';
|
|
|
if(is_string($data)) return $data;
|
|
|
-}
|
|
|
+}
|
|
|
+function commonHttpClient(string $url, array $params, string $requestType='post', string $dataContentType = '',array $headers = [], int $timeout = 5) {
|
|
|
+ try {
|
|
|
+ $client = new Client(['verify' => false,'timeout' => $timeout]);
|
|
|
+ Log::info(request()->secureKey().':http_rqs:'.formatLogData(['url' => $url,'params' => $params,'requestType' => $requestType,'dataContentType' => $dataContentType,'headers' => $headers,'timeout' => $timeout]));
|
|
|
+ if($requestType === 'get'){
|
|
|
+ $response = $client->request('GET', $url.'?'.http_build_query($params));
|
|
|
+ }elseif ($requestType === 'post'){
|
|
|
+ $response = $client->request('POST', $url, [
|
|
|
+ $dataContentType => $params,
|
|
|
+ 'headers' => $headers
|
|
|
+ ]);
|
|
|
+ }else{
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ if($response->getStatusCode() === 200){
|
|
|
+ $body = $response->getBody()->getContents();
|
|
|
+ $result = json_decode($body,true);
|
|
|
+ }else{
|
|
|
+ Log::info(request()->secureKey().':http_response:'.formatLogData($response));
|
|
|
+ $result = [];
|
|
|
+ }
|
|
|
+ //Log::info(request()->secureKey().':http_result:'.formatLogData($result));
|
|
|
+ return $result;
|
|
|
+ } catch (GuzzleException $e) {
|
|
|
+ Log::info(request()->secureKey().':http_error:'.formatLogData($e->getMessage()));
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+}
|