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用法
最新推荐文章于 2025-10-25 16:11:14 发布
本文介绍了一个使用 Guzzle HTTP 客户端进行网络请求的服务封装类。该类提供了一个静态方法,用于通过指定的主机、请求类型、API路径和参数发起HTTP请求。文章中包含了一个测试方法,演示了如何调用此服务来获取横幅广告数据。
5773

被折叠的 条评论
为什么被折叠?



