首页  >  文章  >  后端开发  >  PHP网络请求插件Guzzle使用

PHP网络请求插件Guzzle使用

Guanhui
Guanhui转载
2020-05-01 09:40:575755浏览

在写后台代码时,避免不了需要与其他第三方接口交互,如向服务号下发模板消息,有时可能需要下发超过 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 请求获取自动回答内容,直接上代码:

<?php
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
class GuzzleUseController extends Controller {
    public function tuling(Request $request) {
        $params = [
            &#39;key&#39; => &#39;*****&#39;,
            &#39;userid&#39; => &#39;yemeishu&#39;
        ];
        $params[&#39;info&#39;] = $request->input(&#39;info&#39;, &#39;你好吗&#39;);
        $client = new Client();
        $options = json_encode($params, JSON_UNESCAPED_UNICODE);
        $data = [
            &#39;body&#39; => $options,
            &#39;headers&#39; => [&#39;content-type&#39; => &#39;application/json&#39;]
        ];
        // 发送 post 请求
        $response = $client->post(&#39;http://www.tuling123.com/openapi/api&#39;, $data);
        $callback = json_decode($response->getBody()->getContents());
        return $this->output_json(&#39;200&#39;, &#39;测试图灵机器人返回结果&#39;, $callback);
    }
}

Guzzle client->post 函数还是很简单的,只需要访问的接口,和请求的参数,参数中主要包含:body、headers、query等,具体可参考

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

测试下:

6302-8a23f04d4eb24a12.jpg

6302-2f9f5585e3f021f6.jpg

注:图灵机器人还是很智能的,根据相同的 userid 能够识别上下文,做到智能聊天的。

发送异步的 POST 请求

在 PHP 开发中主要是「面向过程」式的开发方式,但请求第三方接口时,有时候并不需要等待第三方接口返回结果才继续执行。如用户购买成功时,我们需要向短信接口,发送一个 post 请求,由短信平台发送一条短信给用户,告知用户支付成功了,因为这类「提醒消息」属于「额外的附加功能」,并不需要在用户支付时「知道」有没有发送提醒成功。

这时候可以使用 Guzzle 的异步请求功能,直接看代码:

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

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

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

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

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

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

    return $this->output_json(&#39;200&#39;, &#39;测试短信 api&#39;, []);
}

先返回接口数据:

6302-95ec6d7a688f1072.jpg

然后再输出 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"}}}

最后收到短信信息:

微信截图_20200501093336.png

发送多线程异步 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(&#39;sendtemplate:&#39;.$e->getMessage());
                }
            }
        };

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

        $promise = $pool->promise();

        $promise->wait();
    }

其中 request 方法:

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

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

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

        $params[&#39;data&#39;] = $this->formatData($params[&#39;data&#39;]);

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

        $options = json_encode ( $params,  JSON_UNESCAPED_UNICODE);
        $data = [
            &#39;query&#39; => [
                &#39;access_token&#39; => $this->getAccessToken()->getToken()
            ],
            &#39;body&#39; => $options,
            &#39;headers&#39; => [&#39;content-type&#39; => &#39;application/json&#39;]
        ];
        return function() use ($data) {
            return $this->getHttp()->getClient()->requestAsync(&#39;POST&#39;, $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 = &#39;http://127.0.0.1:8126/guzzle-server/perf&#39;;
    for ($i = 0; $i < $total; $i++) {
        yield new Request(&#39;GET&#39;, $uri);
    }};$pool = new Pool($client, $requests(100), [
    &#39;concurrency&#39; => 5,
    &#39;fulfilled&#39; => function ($response, $index) {
        // this is delivered each successful response
    },
    &#39;rejected&#39; => 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中文网其他相关文章!

声明:
本文转载于:jianshu.com。如有侵权,请联系admin@php.cn删除