【TP5】安装Guzzle并简单使用

举报
原来是咔咔 发表于 2022/03/27 23:00:39 2022/03/27
【摘要】 Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。 安装 看到文档的安装方式会不知道怎么弄,因为composer安装列表没有下载方式 (1)打开项目的composer.json文件,添加GuzzleHttp这个安装包 (2)执行composer update ...

Guzzle是一个PHP的HTTP客户端,用来轻而易举地发送请求,并集成到我们的WEB服务上。

安装

看到文档的安装方式会不知道怎么弄,因为composer安装列表没有下载方式

(1)打开项目的composer.json文件,添加GuzzleHttp这个安装包

(2)执行composer update

(3)然后项目就有了

使用GuzzleHttp

(1)引入

(2)请求代码

(3)发起请求,这就是简单的使用

源码

base.php文件


  
  1. <?php
  2. namespace app\huanxin\controller;
  3. use think\config;
  4. use think\Controller;
  5. use GuzzleHttp\Client;
  6. use GuzzleHttp\Exception\RequestException;
  7. class Base extends Controller
  8. {
  9. protected $orgname;
  10. protected $appname;
  11. protected $client_ID;
  12. protected $client_Secret;
  13. protected $request_url;
  14. public function __construct()
  15. {
  16. $this->orgname = config::get('orgname');
  17. $this->appname = config::get('appname');
  18. $this->client_ID = config::get('client_ID');
  19. $this->client_Secret = config::get('client_Secret');
  20. $this->request_url = 'https://a1.easemob.com/';
  21. }
  22. /**
  23. * author:咔咔
  24. *
  25. * 基础请求地址
  26. * @return string
  27. */
  28. public function baseUrl()
  29. {
  30. return $this->request_url . $this->orgname . '/' . $this->appname . '/';
  31. }
  32. /**
  33. * 发送请求
  34. * @param $method 请求方式
  35. * @param array $params 请求参数
  36. * @param $options
  37. * @return array|mixed
  38. * @throws Error
  39. */
  40. public function request($method, $url, $params = [], $options = [])
  41. {
  42. $config = ['base_uri' => $this->baseUrl()];
  43. $client = new Client($config);
  44. $header = [
  45. 'Content-Type' => 'application/json',
  46. 'Accept' => 'application/json',
  47. ];
  48. $data = ['connect_timeout' => 30, 'headers' => $header];
  49. if ($method == 'GET') {
  50. $data['query'] = $params;
  51. } else {
  52. $data['json'] = $params;
  53. }
  54. $options && $data = array_merge($data, $options);
  55. $body = [];
  56. try {
  57. $res = $client->request($method, $url, $data);
  58. $body = \GuzzleHttp\json_decode($res->getBody()->getContents(), 1);
  59. } catch (RequestException $e) {
  60. throw new Error($e->getMessage(), -1);
  61. }
  62. return $body;
  63. }
  64. /**
  65. * author:咔咔
  66. *
  67. * 获取token
  68. * @throws Error
  69. */
  70. public function getTokenFromServer()
  71. {
  72. $data = [
  73. 'grant_type' => 'client_credentials',
  74. 'client_id' => $this->client_ID ,
  75. 'client_secret' => $this->client_Secret
  76. ];
  77. $result = $this->request('POST', 'token', $data);
  78. return $result;
  79. }
  80. }

error.php文件


  
  1. <?php
  2. namespace app\huanxin\controller;
  3. use Exception;
  4. class Error extends \Exception
  5. {
  6. public function __construct($message = "", $code = 0, Exception $previous = null)
  7. {
  8. parent::__construct($this->_getMessage($message), $code, $previous);
  9. }
  10. // TODO 异常信息过滤
  11. private function _getMessage($msg)
  12. {
  13. $message = $msg;
  14. return $message;
  15. }
  16. }

index.php文件


  
  1. <?php
  2. namespace app\huanxin\controller;
  3. use think\config;
  4. class Index extends Base
  5. {
  6. public function index()
  7. {
  8. $data = $this->getTokenFromServer();
  9. dump($data);die;
  10. }
  11. }

项目目录

文章来源: blog.csdn.net,作者:咔咔-,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/fangkang7/article/details/102549000

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。