Mix PHP V2 生态:让 Guzzle 支持 Swoole 的 Hook 协程

栏目: PHP · 发布时间: 7年前

内容简介:Guzzle 是一个非常流行的 PHP 的 HTTP 客户端,现在各大厂的 SDK 也都开始基于 Guzzle 开发,因为 Swoole 只支持 PHP Stream 的协程 Hook ,而 Guzzle 默认是使用 cURL 扩展的,所以使用 Composer 安装:在项目的

Guzzle 是一个非常流行的 PHP 的 HTTP 客户端,现在各大厂的 SDK 也都开始基于 Guzzle 开发,因为 Swoole 只支持 PHP Stream 的协程 Hook ,而 Guzzle 默认是使用 cURL 扩展的,所以 Mix PHP 开发了 Guzzle Hook,能在不修改源码的情况下让 Guzzle 协程化。

Github

安装

使用 Composer 安装:

composer require mix/guzzle-hook

在项目的 composer.json 文件中增加 extra 配置项,如下:

"extra": {
    "include_files": [
      "vendor/mix/guzzle-hook/src/functions_include.php"
    ]
}

使用

直接使用 Guzzle 开发

无需做任何特殊的代码处理,直接根据 Guzzle 文档使用:

go(function () {

    $client = new GuzzleHttp\Client();
    $res    = $client->request('GET', 'https://api.github.com/user', [
        'auth' => ['user', 'pass'],
    ]);
    echo $res->getStatusCode();

});

第三方 SDK 依赖 Guzzle

比如:

这类第三方库从 composer.json 的 require 能看出来依赖了 guzzlehttp/guzzle,则可以在 Swoole 的协程中直接使用。

go(function () {

    try {
        // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey
        $cred = new Credential("secretId", "secretKey");
        // # 实例化要请求产品(以cvm为例)的client对象
        $client = new CvmClient($cred, "ap-guangzhou");
        // 实例化一个请求对象
        $req = new DescribeZonesRequest();
        // 通过client对象调用想要访问的接口,需要传入请求对象
        $resp = $client->DescribeZones($req);
        print_r($resp->toJsonString());
    } catch (TencentCloudSDKException $e) {
        echo $e;
    }

});

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Out of their Minds

Out of their Minds

Dennis Shasha、Cathy Lazere / Springer / 1998-07-02 / USD 16.00

This best-selling book is now available in an inexpensive softcover format. Imagine living during the Renaissance and being able to interview that eras greatest scientists about their inspirations, di......一起来看看 《Out of their Minds》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具