SWOOLE HOOK CURL 同步IO变异步IO

采用 Hook 原生 PHP 函数的方式实现协程客户端,通过一行代码就可以让原来的同步 IO 的代码变成可以协程调度的异步 IO,即一键协程化。
SWOOLE_HOOK_CURL
v4.4LTS 后或 v4.5 开始正式支持。
Swoole:PHP 协程框架

CURL 的 HOOK,支持的函数有:

curl_init
curl_setopt
curl_exec
curl_multi_getcontent
curl_setopt_array
curl_error
curl_getinfo
curl_errno
curl_close
curl_reset
示例:

Co::set(['hook_flags' => SWOOLE_HOOK_CURL]);

Co\run(function () {
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, "http://www.xinhuanet.com/");  
    curl_setopt($ch, CURLOPT_HEADER, false);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);  
    curl_close($ch);
    var_dump($result);
});

相关文章:

1、CentOS 7.x 系统yum环境搭建PHP 7.x + Apache 2.4 + MariaDB 10.x
https://www.onexin.net/solution/centos-7-x-php-7-x-apache-2-4-mariadb-10-x/

2、CentOS 8.2 服务器上yum安装Apache+Php+MariaDB+MongoDB
https://www.onexin.net/latest/centos-8-2-yum-apache-php-mariadb-mongodb/

3、PHP描述冒泡排序和快速排序算法
https://www.onexin.net/languages/php-description-of-bubble-sort-and-quicksort-algorithm/

4、用C/C++扩展你的PHP 为你的php增加功能
https://www.onexin.net/languages/cc-extensions-your-php-to-add-functionality-to-your-php/

5、PHP今天0点UNIX时间戳
https://www.onexin.net/labs/php-0-clock-today-unixtime/

转载请注明出处:https://www.onexin.net/latest/swoole-hook-curl/

Leave a Reply

Your email address will not be published. Required fields are marked *