【TP5】安裝Guzzle並簡單使用

 

Guzzle是一個PHP的HTTP客戶端,用來垂手可得地發送請求,並集成到咱們的WEB服務上。php

安裝

看到文檔的安裝方式會不知道怎麼弄,由於composer安裝列表沒有下載方式json

(1)打開項目的composer.json文件,添加GuzzleHttp這個安裝包app

 

 

(2)執行composer updatecomposer

 

(3)而後項目就有了this

 

使用GuzzleHttp

(1)引入url

 

(2)請求代碼spa

 

(3)發起請求,這就是簡單的使用code

源碼

base.php文件token

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


}

項目目錄

相關文章
相關標籤/搜索