当前位置: 首页 > 后端技术 > PHP

Swoolev4.6.5发布,增加原生curlmulti支持

时间:2023-03-29 14:49:51 PHP

v4.6.5没有向后不兼容的变化,主要对原生curlhook做了一些增强,支持curlmulti,在使用原生curlhook的前提下支持原生curlmulti它就是编译Swoole扩展时开启--enable-swoole-curl选项。可以使用如下代码进行测试://设置URL和相应的选项curl_setopt($ch1,CURLOPT_URL,"http://www.baidu.com/");curl_setopt($ch1,CURLOPT_HEADER,0);curl_setopt($ch1,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch2,CURLOPT_URL,"http://www.gov.cn/");curl_setopt($ch2,CURLOPT_HEADER,0);curl_setopt($ch2,CURLOPT_RETURNTRANSFER,1);$mh=curl_multi_init();curl_multi_add_handle($mh,$ch1);curl_multi_add_handle($mh,$ch2);$active=null;//执行批处理do{$mrc=curl_multi_exec($mh,$active);}while($mrc==CURLM_CALL_MULTI_PERFORM);while($active&&$mrc==CURLM_OK){$n=curl_multi_select($mh);if($n!=-1){do{$mrc=curl_multi_exec($mh,$active);}while($mrc==CURLM_CALL_MULTI_PERFORM);}}$info1=curl_multi_info_read($mh);$info2=curl_multi_info_read($mh);$info3=curl_multi_info_read($mh);断言($info1['msg']===CURLMSG_DONE);断言($info2['msg']===CURLMSG_DONE);断言($info3===false);assert(strpos(curl_multi_getcontent($ch1),'baidu.com')!==false);assert(strpos(curl_multi_getcontent($ch2),'中央人民政府门户网站')!==false);curl_multi_remove_handle($mh,$ch1);curl_multi_remove_handle($mh,$ch2);curl_multi_close($mh);});支持curlmulti之后,也间接支持Guzzle,不用改任何代码,准备支持包括__DIR__。'/vendor/autoload.php';使用Swoole\Coroutine\Barrier;使用Swoole\Runtime;使用GuzzleHttp\Client;使用GuzzleHttp\Promise;使用函数Swoole\Coroutine\run;使用函数Swoole\Coroutine\go;Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);constN=4;run(function(){$barrier=Barrier::make();$result=[];go(function()use($barrier,&$result){$client=newClient();$promises=['baidu'=>$client->getAsync('http://www.baidu.com/'),'qq'=>$client->getAsync('https://www.qq.com/'),'gov'=>$client->getAsync('http://www.gov.cn/')];$responses=Promise\Utils::unwrap($promises);断言(strpos($responses['baidu']->getBody(),'百度')!==false);assert(strpos(iconv('gbk','utf-8',$responses['qq']->getBody(),'腾讯')!==false);assert(strpos($responses['gov']->getBody(),'中华人民共和国')!==false);$result['task_1']='确定';});go(function()use($barrier,&$result){$client=newClient(['base_uri'=>'http://httpbin.org/']);$n=N;$data=$promises=[];while($n--){$key='req_'.$n;$data[$key]=uniqid('swoole_test');$promises[$key]=$client->getAsync('/base64/'.base64_encode($data[$key]));}$responses=Promise\Utils::unwrap($promises);$n=N;while($n--){$key='req_'.$n;assert($responses[$key]->getBody()===$data[$key]);}$result['task_2']='OK';});屏障::等待($屏障);assert($result['task_1']==='OK');assert($result['task_2']==='OK');回声“完成”。PHP_EOL;});还添加了一些Guzzle单元测试,以允许使用数组在使用HTTP/2的响应中设置标头。从v4.6.0开始,Swoole\Http\Response支持重复设置相同的$keyHTTPHeader,$value支持多种类型,比如array,object,int,float,底层会进行toString转换,会去掉最后的空格和换行符。但是,不支持HTTP/2。有关详细信息,请参阅问题#4133。这个版本也支持:$http=newSwoole\Http\Server('127.0.0.1',9501);$http->set(['open_http2_protocol'=>true]);$http->on('request',function($request,$response){$response->header('测试值',["a\r\n",'d5678',"e\n",null,5678,3.1415926,]);$response->end("

HelloSwoole.#".rand(1000,9999)."

");});$http->开始();可以使用上面的代码进行测试,使用curl命令测试结果$curl--http2-prior-knowledge-vhttp://localhost:9501*Trying::1...*TCP_NODELAYset*Connectionfailed*连接到::1端口9501失败:连接被拒绝*尝试127.0.0.1...*设置TCP_NODELAY*连接到本地主机(127.0.0.1)端口9501(#0)*使用HTTP2,服务器支持多用途*连接状态已更改(确认HTTP/2)*升级后将流缓冲区中的HTTP/2数据复制到连接缓冲区:len=0*使用流ID:1(easyhandle0x7fe9e9009200)>GET/HTTP/2>Host:localhost:9501>User-Agent:curl/7.64.1>Accept:*/*>*连接状态已更改(MAX_CONCURRENT_STREAMS==128)!你好,Swoole。#6944*Closingconnection0Updatelog以下是完整的更新日志:NewAPIaddscountmethodinWaitGroup(swoole/library#100)(@sy-records)(@deminy)Enhancednativecurlmulti(#4093)(#4099)(#4101)(#4105)(#4113)(#4121)(#4147)(swoole/swoole-src@cd7f51c)(@matyhtf)(@sy-records)(@huanghantao)允许使用HTTP/2修复在响应中使用数组设置标头修复NetBSD构建(#4080)(@devnexen)修复OpenBSD构建(#4108)(@devnexen)修复仅使用成员别名的illumos/solaris构建(#4109)(@devnexen)修复握手未完成时SSL连接心跳不工作的问题(#4114)(@matyhtf)修复Http\Client使用Thereisanerrorgeneratedbyhost:portinthehostwhenproxying(#4124)(@Yurunsoft)FixheaderandcookiesettinginSwoole\Coroutine\Http::request(swoole/library#103)(@leocavalcante)(@deminy)kernelsupportsasmcontextonBSD(#4082)(@devnexen)在FreeBSD下使用arc4random\_buf实现getrandom(#4096)(@devnexen)优化darwinarm64上下文:删除解决方法使用标签(#4127)(@devnexen)测试添加alpine构建脚本(#4104)(@limingxinleo)