【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;
    }


}

项目目录

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咔咔-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值