您好,欢迎访问一九零五行业门户网

可用于实现纯异步PHP程序:php_http_parser

php_http_parser 是基于node.js http-parser的php扩展,可用于实现纯异步php程序
libcurl提供了异步调用方式,有两种风格:
one multi handle many easy handles:加入多个easy handle后执行curl_multi_perform方法。该方法在php curl扩展中有对应实现。但最后一步curl_multi_perform是阻塞的。
multi_socket,这个是真正的非阻塞方法,但需要自行实现event loop,且封装较为困难,目前在php中没有对应实现。经过调研,curl_multi_socket_action跟php内核结合困难度很高。
除此之外,基本上没有真正的实现异步http请求的php扩展。目前仅有部分纯php实现的版本,比如tsf中的http client实现。 使用纯php实现的问题主要受限于http解析的性能。因此考虑将这一模块用扩展的方式来实现。node.js http-parser就是一个很好的c语言的http解析库。 php_http_parser就是对其做的一个封装,在php中暴露出相应的接口。
为了实现真正的非阻塞请求,仍然需要自己实现event loop。目前推荐结合swoole使用,以获得更好的性能。
使用方式 $buffs = array(http/1.1 301 moved permanently\r\n,location: http://www.google.com/\r\n,content-type: text/html; charset=utf-8\r\n,date: sun, 26 apr 2009 11:11:49 gmt\r\n,expires: tue, 26 may 2009 11:11:49 gmt\r\n,cache-control: public, max-age=2592000\r\n,server: gws\r\n,content-length: 193\r\n,\r\n,\n,301 moved\n,301 moved\n,the document has moved\n,here.\r\n ,here.\r\n ,here.\r\n ,here.\r\n ,here.\r\n,\r\n);$hp = new httpparser();foreach($buffs as $buff){ $ret = $hp->execute($buff); if($ret !== false){ echo $ret; break; }}
虽然http请求可能分包发送,httpparser会将所有包合并在一起后,出发body事件,然后调用相应的回调方法。 诸如header回调,目前暂未实现。另外,此处需要自行实现timeout逻辑。
示例代码是结合swoole_client与swpromise框架实现的一个异步http client。 籍此可以实现真正的非阻塞的php程序。
class httpclientfuture implements futureintf { protected $url = null; protected $post = null; protected $proxy = false; public function __construct($url, $post = array(), $proxy = array()) { $this->url = $url; $this->post = $post; if($proxy){ $this->proxy = $proxy; } } public function run(promise &$promise) { $cli = new \swoole_client ( swoole_tcp, swoole_sock_async ); $urlinfo = parse_url ( $this->url ); if(!isset($urlinfo ['port']))$urlinfo ['port'] = 80; $httpparser = new \httpparser(); $cli->on ( connect, function ($cli)use($urlinfo){ $host = $urlinfo['host']; if($urlinfo['port'])$host .= ':'.$urlinfo['port']; $req = array(); $req[] = get {$this->url} http/1.1\r\n; $req[] = user-agent: php swasync\r\n; $req[] = host:{$host}\r\n; $req[] = connection:close\r\n; $req[] = \r\n; $req = implode('', $req); $cli->send ( $req ); } ); $cli->on ( receive, function ($cli, $data = ) use(&$httpparser, &$promise) { $ret = $httpparser->execute($data); if($ret !== false){ $cli->close(); $promise->accept(['http_data'=>$ret]); } } ); $cli->on ( error, function ($cli) use(&$promise) { $promise->reject (); } ); $cli->on ( close, function ($cli) { } ); if($this->proxy){ $cli->connect ( $this->proxy['host'], $this->proxy ['port'], 1 ); }else{ $cli->connect ( $urlinfo ['host'], $urlinfo ['port'], 1 ); } }}
性能 [web@gz-web01 php_http_parser]$ time /data/server/php/bin/php http_parser.php 2000000real 0m11.489suser 0m11.435ssys 0m0.017s
1个worker进程
./http_load -fetches 20000 -parallel 100 9502.listasync 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 5.94536 seconds101 mean bytes/connection3363.97 fetches/sec, 339761 bytes/secmsecs/connect: 0.0473873 mean, 1.155 max, 0.019 minmsecs/first-response: 29.6366 mean, 51.736 max, 15.22 minhttp response codes:code 200 -- 20000-bash: history: write error: success
2个worker进程
./http_load -fetches 20000 -parallel 100 9502.listasync 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 3.17119 seconds101 mean bytes/connection6306.77 fetches/sec, 636984 bytes/secmsecs/connect: 0.0643583 mean, 1.211 max, 0.023 minmsecs/first-response: 15.7489 mean, 32.425 max, 3.242 minhttp response codes:code 200 -- 20000-bash: history: write error: success
4个woker进程
./http_load -fetches 20000 -parallel 100 9502.listasync 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 1.57194 seconds101 mean bytes/connection12723.2 fetches/sec, 1.28504e+06 bytes/secmsecs/connect: 0.0815263 mean, 1.349 max, 0.02 minmsecs/first-response: 7.65904 mean, 22.568 max, 1.221 minhttp response codes:code 200 -- 20000-bash: history: write error: success
8个woker进程
./http_load -fetches 20000 -parallel 100 9502.listasync 20000 fetches, 100 max parallel, 2.02e+06 bytes, in 1.02967 seconds101 mean bytes/connection19423.8 fetches/sec, 1.9618e+06 bytes/secmsecs/connect: 0.147502 mean, 1.575 max, 0.014 minmsecs/first-response: 3.17218 mean, 22.566 max, 0.339 minhttp response codes:code 200 -- 20000-bash: history: write error: success
官方网站:http://www.open-open.com/lib/view/home/1451808838620
其它类似信息

推荐信息