laravel GuzzleHttp用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/PerfectUrl/article/details/84826415
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();
    }

猜你喜欢

转载自blog.csdn.net/PerfectUrl/article/details/84826415