Mix PHP V2 生态:让 Guzzle 支持 Swoole 的 Hook 协程
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 文档使用:
// Mix PHP 中是 xgo ,原生 swoole 是 go
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 的协程中直接使用。
// Mix PHP 中是 xgo ,原生 swoole 是 go
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;
    }
}); 相关推荐
  efeve    2020-09-14  
   ericxieforever    2020-09-03  
   hohohogetitup    2020-06-19  
   Dimples    2020-06-08  
   鲁鲁酱    2020-06-02  
   feishicheng    2020-05-31  
   paopao00    2020-05-10  
   georgeandgeorge    2020-05-09  
   Greatemperor    2020-05-03  
   jacktangj    2020-04-17  
   狼窝    2020-04-07  
   CloudXli    2020-04-07  
   oXiaoChong    2020-04-07  
   DanielMan    2020-02-19  
   学习备忘录    2020-02-18  
   wyqwilliam    2020-02-10  
   ZGCdemo    2020-01-17  
 