PHP網路請求插件Guzzle使用

Guanhui
發布: 2023-04-08 15:32:02
轉載
5654 人瀏覽過

在寫後台程式碼時,避免不了需要與其他第三方介面交互,如向服務號下發範本訊息,有時可能需要下發超過 10 萬條。這時不得不考慮使用非同步和「多線程」的網路請求。

今天向 PHP 工程師推薦一個 Guzzle 外掛。

Guzzle

Guzzle 是一個 PHP 的 HTTP 用戶端,用來輕易地傳送請求,並整合到我們的 WEB 服務上。

介面簡單:建立查詢語句、POST 請求、分流上傳下載大檔案、使用 HTTP cookies、上傳 JSON 資料等等。

傳送同步或非同步的請求皆使用相同的介面。

使用 PSR-7 介面來請求、回應、分流,讓你可以使用其他相容的 PSR-7 類別庫與 Guzzle 共同開發。

抽象化了底層的 HTTP 傳輸,允許你改變環境以及其他的程式碼,如:對 cURL與 PHP 的流或 socket 並非重度依賴,非阻塞事件循環。

中間件系統允許你創建構成客戶端行為。

安裝 Guzzle

本文結合 Laravel 專案介紹 Guzzle 基本使用,所以使用 composer 來安裝 Guzzle 再適合不過了,而且 Guzzle 官網也推薦使用 composer 來安裝。

composer require guzzlehttp/guzzle:~6.0
// 或者
php composer.phar require guzzlehttp/guzzle:~6.0
登入後複製

發送簡單的 POST 請求

訪問第三方接口,基本上都是 POST 請求為主。如你想做一個簡單的智慧聊天工具,這時候可以藉助圖靈機器人API,發送一個POST 請求獲取自動回答內容,直接上代碼:

 '*****',
            'userid' => 'yemeishu'
        ];
        $params['info'] = $request->input('info', '你好吗');
        $client = new Client();
        $options = json_encode($params, JSON_UNESCAPED_UNICODE);
        $data = [
            'body' => $options,
            'headers' => ['content-type' => 'application/json']
        ];
        // 发送 post 请求
        $response = $client->post('http://www.tuling123.com/openapi/api', $data);
        $callback = json_decode($response->getBody()->getContents());
        return $this->output_json('200', '测试图灵机器人返回结果', $callback);
    }
}
登入後複製

Guzzle client->post 函數還是很簡單的,只需要存取的接口,和請求的參數,參數中主要包含:body、headers、query等,具體可參考

http://guzzle-cn.readthedocs.io/zh_CN /latest/quickstart.html#id8

測試下:

PHP網路請求插件Guzzle使用

PHP網路請求插件Guzzle使用

#:圖靈機器人還是很聰明的,根據相同的userid 能夠辨識上下文,做到智慧聊天的。

發送非同步的POST 請求

在PHP 開發中主要是「過程導向」式的開發方式,但請求第三方介面時,有時並不需要等待第三方介面回傳結果才繼續執行。如用戶購買成功時,我們需要向短信接口,發送一個post 請求,由短信平台發送一條短信給用戶,告知用戶支付成功了,因為這類“提醒消息”屬於“額外的附加功能”,並不需要在用戶付款時「知道」有沒有發送提醒成功。

這時候可以使用Guzzle 的非同步請求功能,直接看程式碼:

public function sms(Request $request) {
    $code = $request->input('code');
    $client = new Client();
    $sid = '9815b4a2bb6d5******8bdb1828644f2';
    $time = '20171029173312';
    $token = 'af8728c8bc*******12019c680df4b11c';

    $sig =  strtoupper(md5($sid.$token.$time));

    $auth = trim(base64_encode($sid . ":" . $time));

    $params = ['templateSMS' => [
            'appId' => '12b43**********0091c73c0ab',
            'param' => "coding01,$code,30",
            'templateId' => '3***3',
            'to' => '17689974321'
        ]
    ];
    $options = json_encode($params, JSON_UNESCAPED_UNICODE);
    $data = [
        'query' => [
            'sig' => $sig
        ],
        'body' => $options,
        'headers' => [
            'content-type' => 'application/json',
            'Authorization' => $auth
        ]
    ];

    // 发送 post 请求
    $promise = $client->requestAsync('POST', 'https://api.ucpaas.com/2014-06-30/Accounts/9815b4a2bb6d5******8bdb1828644f2/Messages/templateSMS', $data);

    $promise->then(
        function (ResponseInterface $res) {
            Log::info('---');
            Log::info($res->getStatusCode() . "\n");
            Log::info($res->getBody()->getContents() . "\n");
        },
        function (RequestException $e) {
            Log::info('-__-');
            Log::info($e->getMessage() . "\n");
        }
    );
    $promise->wait();

    return $this->output_json('200', '测试短信 api', []);
}
登入後複製

先回傳介面資料:

PHP網路請求插件Guzzle使用

##然後再輸出Log:

[2017-10-29 09:53:14] local.INFO: ---  
[2017-10-29 09:53:14] local.INFO: 200
  
[2017-10-29 09:53:14] local.INFO: {"resp":{"respCode":"000000","templateSMS":{"createDate":"20171029175314","smsId":"24a93f323c9*****8608568"}}}
登入後複製

最後收到簡訊訊息:

PHP網路請求插件Guzzle使用

#發送多執行緒非同步POST 請求

「傳送多執行緒非同步POST 請求」在許多場合中使用到的,如:雙十一快到了,可以做一些回饋老用戶的活動,這是就需要批量的向老用戶推送一條模板訊息,告訴用戶參與哪些活動的。這時候就需要用到多執行緒非同步請求微信公眾號介面。

直接上程式碼:

public function send($templateid, $openid, $url, $data) {
        $client = $this->bnotice->getHttp()->getClient();

        $requests = function ($open_ids) use ($templateid, $url, $data) {
            foreach($open_ids as $v){
                try {
                    yield $this->bnotice
                        ->template($templateid)
                        ->to($v)
                        ->url($url)
                        ->data($data)
                        ->request();
                } catch(Exception $e) {
                    Log::error('sendtemplate:'.$e->getMessage());
                }
            }
        };

        $pool = new Pool($client, $requests($openid), [
            'concurrency' => 16,
            'fulfilled' => function ($response, $index) {
            },
            'rejected' => function ($reason, $index) {
            },
        ]);

        $promise = $pool->promise();

        $promise->wait();
    }
登入後複製

其中request 方法:

public function request($data = [])
    {
        $params = array_merge([
            'touser' => '',
            'template_id' => '',
            'url' => '',
            'topcolor' => '',
            'miniprogram' => [],
            'data' => [],
        ], $data);
        
        $required = ['touser', 'template_id'];

        foreach ($params as $key => $value) {
            if (in_array($key, $required, true) && empty($value) && empty($this->message[$key])) {
                throw new InvalidArgumentException("Attribute '$key' can not be empty!");
            }

            $params[$key] = empty($value) ? $this->message[$key] : $value;
        }

        $params['data'] = $this->formatData($params['data']);

        $this->message = $this->messageBackup;

        $options = json_encode ( $params,  JSON_UNESCAPED_UNICODE);
        $data = [
            'query' => [
                'access_token' => $this->getAccessToken()->getToken()
            ],
            'body' => $options,
            'headers' => ['content-type' => 'application/json']
        ];
        return function() use ($data) {
            return $this->getHttp()->getClient()->requestAsync('POST', $this::API_SEND_NOTICE, $data);
        };
    }
登入後複製

Guzzle 多執行緒非同步請求原型函數,使用GuzzleHttp\Pool 物件

use GuzzleHttp\Pool;use GuzzleHttp\Client;use GuzzleHttp\Psr7\Request;$client = new Client();$requests = function ($total) {
    $uri = 'http://127.0.0.1:8126/guzzle-server/perf';
    for ($i = 0; $i < $total; $i++) {
        yield new Request('GET', $uri);
    }};$pool = new Pool($client, $requests(100), [
    'concurrency' => 5,
    'fulfilled' => function ($response, $index) {
        // this is delivered each successful response
    },
    'rejected' => function ($reason, $index) {
        // this is delivered each failed request
    },]);// Initiate the transfers and create a promise$promise = $pool->promise();// Force the pool of requests to complete.$promise->wait();
登入後複製

#總結

有了Guzzle,極大方便了我們並發非同步請求第三方介面。如果時間允許,我們可以看看 Guzzle 原始碼,看看是如何實現的。

推薦教學:《

PHP教學

以上是PHP網路請求插件Guzzle使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:jianshu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!