PHP的HTTP解析擴充:php_http_parser

來源:互聯網
上載者:User
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 );        }    }}

項目首頁:http://www.open-open.com/lib/view/home/1448201436622

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.