如何在Laravel项目中用GuzzleHttp库调用第三方API接口实现长尾词查询?

2026-04-01 07:371阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计447个文字,预计阅读时间需要2分钟。

如何在Laravel项目中用GuzzleHttp库调用第三方API接口实现长尾词查询?

背景:使用Laravel进行分布式开发,自己编写了一个业务系统,还写了一个用户中心和其它信息中心。现在前端只需要访问业务系统的API接口即可获取到其它服务上的数据。

背景:用laravel进行分布式开发,自己写了一个业务系统,还写了一个用户中心和其他的信息中心

现在需要做到前端只需要访问业务系统的API接口也可以获取到其他服务上面的数据

如何在Laravel项目中用GuzzleHttp库调用第三方API接口实现长尾词查询?

找了很多资料,最后查到了Laravel自带的GuzzleHttp可以达到我的需求

Guzzle中文文档:

guzzle-cn.readthedocs.io/zh_CN/latest/index.html

引入安装

在composer.json文件的“require”项中加入

"guzzle192.168.31.XX:xxx/api/']); $res = $client->request('POST', $apiStr, ['json' => $body, 'headers' => [ 'Content-type'=> 'application/json', // 'Cookie'=> 'XDEBUG_SESSION=PHPSTORM', "Accept"=>"application/json"] ]); $data = $res->getBody()->getContents(); return $data; } public function get($apiStr,$header) { $client = new \GuzzleHttp\Client(['base_uri' => '192.168.31.XX:xxx/api/']); $res = $client->request('GET', $apiStr,['headers' => $header]); $statusCode= $res->getStatusCode(); $header= $res->getHeader('content-type'); $data = $res->getBody(); return $data; } }

在项目中主要我用的是post方法,

'Cookie'=> 'XDEBUG_SESSION=PHPSTORM',

这一行加进去之后可以使用XDebug进行调试,但是在真正用起来的时候不需要在header里面加这一行了

如果是调用xxx.xxx.com/api/']); $res = $client->request('POST', $apiStr, ['verify' => false, 'json' => $body, 'headers' => [ 'Content-type'=> 'application/json'] ]); $data = $res->getBody()->getContents(); $response=json_decode($data); return $response; }

2、具体在Controller中使用

public function index(Request $request) { $data = $request->json()->all(); $body = $data; $apiStr = '/api/xxx/list'; $api = new APIHelper(); $res =$api->post($body,$apiStr); $data = json_decode($res); $ret=new RetObject(); $ret->retCode='0000'; $ret->retMsg='Success'; $ret->data=$data; return response()->json($ret); }

这样就可以在一个系统里用GuzzleHttp调用第三方的API接口了

以上这篇在Laravel中使用GuzzleHttp调用第三方服务的API接口代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计447个文字,预计阅读时间需要2分钟。

如何在Laravel项目中用GuzzleHttp库调用第三方API接口实现长尾词查询?

背景:使用Laravel进行分布式开发,自己编写了一个业务系统,还写了一个用户中心和其它信息中心。现在前端只需要访问业务系统的API接口即可获取到其它服务上的数据。

背景:用laravel进行分布式开发,自己写了一个业务系统,还写了一个用户中心和其他的信息中心

现在需要做到前端只需要访问业务系统的API接口也可以获取到其他服务上面的数据

如何在Laravel项目中用GuzzleHttp库调用第三方API接口实现长尾词查询?

找了很多资料,最后查到了Laravel自带的GuzzleHttp可以达到我的需求

Guzzle中文文档:

guzzle-cn.readthedocs.io/zh_CN/latest/index.html

引入安装

在composer.json文件的“require”项中加入

"guzzle192.168.31.XX:xxx/api/']); $res = $client->request('POST', $apiStr, ['json' => $body, 'headers' => [ 'Content-type'=> 'application/json', // 'Cookie'=> 'XDEBUG_SESSION=PHPSTORM', "Accept"=>"application/json"] ]); $data = $res->getBody()->getContents(); return $data; } public function get($apiStr,$header) { $client = new \GuzzleHttp\Client(['base_uri' => '192.168.31.XX:xxx/api/']); $res = $client->request('GET', $apiStr,['headers' => $header]); $statusCode= $res->getStatusCode(); $header= $res->getHeader('content-type'); $data = $res->getBody(); return $data; } }

在项目中主要我用的是post方法,

'Cookie'=> 'XDEBUG_SESSION=PHPSTORM',

这一行加进去之后可以使用XDebug进行调试,但是在真正用起来的时候不需要在header里面加这一行了

如果是调用xxx.xxx.com/api/']); $res = $client->request('POST', $apiStr, ['verify' => false, 'json' => $body, 'headers' => [ 'Content-type'=> 'application/json'] ]); $data = $res->getBody()->getContents(); $response=json_decode($data); return $response; }

2、具体在Controller中使用

public function index(Request $request) { $data = $request->json()->all(); $body = $data; $apiStr = '/api/xxx/list'; $api = new APIHelper(); $res =$api->post($body,$apiStr); $data = json_decode($res); $ret=new RetObject(); $ret->retCode='0000'; $ret->retMsg='Success'; $ret->data=$data; return response()->json($ret); }

这样就可以在一个系统里用GuzzleHttp调用第三方的API接口了

以上这篇在Laravel中使用GuzzleHttp调用第三方服务的API接口代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。