laravel请求第三方外部接口api

发布时间:2020-11-19 23:15:47 阅读:2186次

我们知道在php中可以通过file_get_contents来请求外部接口

我们也可以通过curl来请求第三方api

laravel中还可以通过GuzzleHttp来请求

首先安装guzzle

执行以下代码

composer require guzzlehttp/guzzle

controller中加入以下代码

public function index()
{
     try{
          $endpoint = "http://www.test.com";
          $client = new \GuzzleHttp\Client();
          $id = 5;
          $value = "ABC";
          $response = $client->request('post', $endpoint, ['query' => [
              'key1' => $id,
              'key2' => $value
          ]]);
          $statusCode = $response->getStatusCode();
          echo $statusCode;
          $content = $response->getBody();
          echo $content;
      }catch(\Exception $e){
          echo 'Message:' .$e->getMessage();
      }
}

我们可以获得第三方接口状态码返回内容

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:laravel请求第三方外部接口api 出自老鄢博客 | 欢迎分享