【TP5】安装Guzzle并简单使用

简介: 【TP5】安装Guzzle并简单使用

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

安装

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

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

(2)执行composer update

(3)然后项目就有了


使用GuzzleHttp

(1)引入


(2)请求代码


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

源码

base.php文件

<?php
namespace app\huanxin\controller;
use think\config;
use think\Controller;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
class Base extends Controller
{
    protected $orgname;
    protected $appname;
    protected $client_ID;
    protected $client_Secret;
    protected $request_url;
    public function __construct()
    {
        $this->orgname       = config::get('orgname');
        $this->appname       = config::get('appname');
        $this->client_ID     = config::get('client_ID');
        $this->client_Secret = config::get('client_Secret');
        $this->request_url   = 'https://a1.easemob.com/';
    }
    /**
     * author:咔咔
     *
     * 基础请求地址
     * @return string
     */
    public function baseUrl()
    {
        return $this->request_url . $this->orgname . '/' . $this->appname . '/';
    }
    /**
     * 发送请求
     * @param $method   请求方式
     * @param array $params  请求参数
     * @param $options
     * @return array|mixed
     * @throws Error
     */
    public function request($method, $url, $params = [], $options = [])
    {
        $config = ['base_uri' => $this->baseUrl()];
        $client = new Client($config);
        $header = [
            'Content-Type' => 'application/json',
            'Accept'       => 'application/json',
        ];
        $data = ['connect_timeout' => 30, 'headers' => $header];
        if ($method == 'GET') {
            $data['query'] = $params;
        } else {
            $data['json'] = $params;
        }
        $options && $data = array_merge($data, $options);
        $body = [];
        try {
            $res = $client->request($method, $url, $data);
            $body = \GuzzleHttp\json_decode($res->getBody()->getContents(), 1);
        } catch (RequestException $e) {
            throw new Error($e->getMessage(), -1);
        }
        return $body;
    }
    /**
     * author:咔咔
     *
     * 获取token
     * @throws Error
     */
    public function getTokenFromServer()
    {
        $data   = [
            'grant_type'    => 'client_credentials',
            'client_id'     => $this->client_ID ,
            'client_secret' => $this->client_Secret
        ];
        $result = $this->request('POST', 'token', $data);
        return $result;
    }
}

error.php文件

<?php
namespace app\huanxin\controller;
use Exception;
class Error extends \Exception
{
    public function __construct($message = "", $code = 0, Exception $previous = null)
    {
        parent::__construct($this->_getMessage($message), $code, $previous);
    }
    // TODO 异常信息过滤
    private function _getMessage($msg)
    {
        $message = $msg;
        return $message;
    }
}

index.php文件

<?php
namespace app\huanxin\controller;
use think\config;
class Index extends Base
{
    public function index()
    {
        $data = $this->getTokenFromServer();
        dump($data);die;
    }
}

项目目录:

image.png

相关文章
|
4月前
|
Linux API 开发者
​geth命令参数详解 1.10.4​
​geth命令参数详解 1.10.4​
38 0
|
5月前
|
C语言
转载 - gcc/ld 动态连接库和静态连接库使用方法
本文介绍了如何在GCC中实现部分程序静态链接、部分动态链接。使用`-Wl`标志传递链接器参数,`-Bstatic`强制链接静态库,`-Bdynamic`强制链接动态库。
|
5月前
|
JSON 安全 数据安全/隐私保护
​iOS Class Guard github用法、工作原理和安装详解及使用经验总结
​iOS Class Guard github用法、工作原理和安装详解及使用经验总结
91 0
|
XML 消息中间件 小程序
tp项目常用工具,composer
tp项目常用工具,composer
145 0
|
PHP
tp5源码解析--自动加载类
在TP5的框架使用过程中,自动加载类是可能会接触到,上手不难,但若想随心所欲的用,还是需要了解一番。用了千次,却没看过一次源码,学习源码,起码对TP5这个框架使用更加得心应手,毕竟技术服务于业务,能够写出更简介、更方便、更有效的业务代码,本身就是一件身心愉悦的事儿;
128 0
|
安全 应用服务中间件 PHP
TP5.0.20 - TP5更改网站目录为/public 后运行出错解决
当程序要使用例如fopen()或file_get_contents()打开一个文件时,这个文件的位置将会被检查。当文件在指定的目录树之外,程序将拒绝打开。 本指令不受安全模式打开或关闭的影响。
305 0
|
Linux Shell
nc工具基本使用
nc工具基本使用
382 0
|
PHP
【TP5】自定义命令行
【TP5】自定义命令行
142 0
【TP5】自定义命令行
|
中间件 容器
【TP5.1】配置解释大全
【TP5.1】配置解释大全
172 0
【TP5.1】配置解释大全