laravel guzzle 发起请求

1、安装guzzle

composer require guzzlehttp/guzzle

2、引入

use GuzzleHttp\Client;

3、发起请求

        //发起Http请求  并设置超时时间
        $client = new Client(['timeout'=>5]);
        //得到URL地址
        $url=config('gaode.geocode');
        //替换占位符$url=sprintf($url,$model->fang_addr,$city->name);
        //发起请求
        $response=$client->get($url);
        //可尝试 打印$response看看
        $body=(string)$response->getBody();
//格式化
$arr=json_decode($body,true);
     /*
      * 对数据进行处理
     */      

 

posted @ 2020-04-25 19:07  养猪至富  阅读(1079)  评论(0)    收藏  举报