namespace App\Services; use GuzzleHttp\Client; class BannerService { public static function guzzleHttpByGroup($groupHost, $type, $apiPath, $params) { $client = new Client(); //$res = $client->request($type, $groupHost . $apiPath, ['query' => $params]); try { $res = $client->request($type, $groupHost . $apiPath, ['query' => $params]); } catch (\Exception $e) { $errCode = $e->getCode(); if ($errCode == 0) { $errCode = -1; } return 0;//返回错误,根据需求处理 } $body = $res->getBody(); $remainingBytes = $body->getContents(); return json_decode($remainingBytes, true); } //测试类 public static function testBanner(){ $type = "GET";//method $groupHost = "https://dev-www.xxxx.exchange";//host $apiPath = "/api/banner/homeHeader";//api $param['lang'] = "zh-cn";//参数数组 $result = self::guzzleHttpByGroup($groupHost, $type, $apiPath, $param); dd($result); exit(); }
laravel GuzzleHttp用法
最新推荐文章于 2023-11-12 17:30:01 发布