Swoole½ø½×£ºÊ¹ÓÃг̾ÙÐÐWebÅÀ³æ¿ª·¢
Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬webÅÀ³æÒѾ³ÉΪµ±½ñ»¥ÁªÍøÓ¦Óò»¿É»òȱµÄÒ»²¿·Ö£¬ÆäÔÚÊý¾ÝÊÕÂÞ¡¢ÓªÒµ¾ò¿Í¡¢ÓßÇé¼à²âµÈ·½Ã涼ÓÐÆÕ±éµÄÓ¦Óó¡¾°¡£È»¶ø¹Å°åµÄwebÅÀ³æͨ³£Ê¹ÓöàÏ̻߳ò¶àÀú³ÌÀ´ÊµÏÖ²¢·¢ÇëÇó£¬ÃæÁÙµÄÎÊÌâ°üÀ¨ÉÏÏÂÎÄÇл»¿ªÏú¡¢ÄÚ´æÕ¼Óùý´óµÈ¡£¶ø½üÄêÀ´£¬swoole³ÉΪphpÓ¦ÓÃÖеÄÒ»¿ÅÐÂÐÇ£¬ËüµÄгÌÌØÕ÷¿ÉÒÔΪwebÅÀ³æµÄ²¢·¢ÇëÇóÌṩ¸ßЧµÄ½â¾ö¼Æ»®¡£
ÔÚ±¾ÎÄÖУ¬½«ÏÈÈÝÔõÑùʹÓÃSwooleгÌʵÏÖÇáÁ¿¼¶¡¢¸ßЧµÄWebÅÀ³æ¡£
Swoole¼ò½é
SwooleÊÇ»ùÓÚPHPÓïÑÔʵÏֵĸßÐÔÄÜÍøÂçͨѶ¿ò¼Ü£¬Æä×î´óµÄÌصãÊÇÖ§³Öг̡£Ð³ÌÊÇÒ»ÖÖÓû§Ì¬µÄÇáÁ¿¼¶Ị̈߳¬Óë¹Å°åµÄÏ̺߳ÍÀú³ÌÏà±È£¬Ð³ÌµÄÉÏÏÂÎÄÇл»¿ªÏúС¡¢ÄÚ´æÕ¼ÓÃÉÙ£¬¿ÉÒÔ¸üºÃµØÊ©Õ¹CPUµÄÐÔÄÜ¡£
ʹÓÃSwooleʵÏÖWebÅÀ³æ
SwooleµÄгÌÌØÕ÷ΪWebÅÀ³æµÄ¿ª·¢ÌṩÁËÒ»¸öºÜÊǺõÄƽ̨¡£¹Å°åµÄWebÅÀ³æÔÚ²¢·¢ÇëÇóʱÍùÍùÐèÒªÏûºÄ´ó×ÚµÄϵͳ×ÊÔ´£¬¶øʹÓÃSwooleг̿ÉÒÔÇáËÉʵÏָ߲¢·¢ÇëÇó£¬Í¬Ê±»¹ÄÜ×èÖ¹¹Å°åµÄÏß³ÌÇл»´øÀ´µÄ¿ªÏú¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʹÓÃSwooleʵÏÖµÄWebÅÀ³æʾÀý£º
<?php // 1. ½¨ÉèSwoole HTTPЧÀÍÆ÷ $http = new SwooleHttpServer("0.0.0.0", 9501); // 2. ´¦ÀíÇëÇó $http->on('request', function ($request, $response) { // 3. ·¢ËÍHTTPÇëÇó $cli = new SwooleCoroutineHttpClient('www.www.zonelele.com', 80); $cli->setHeaders([ 'Host' => "www.www.zonelele.com", "User-Agent" => 'Chrome/49.0.2587.3', 'Accept' => 'text/html,application/xhtml+xml,application/xml', 'Accept-Encoding' => 'gzip', ]); $cli->get('/'); // 4. ÏìÓ¦HTMLÄÚÈÝ $response->header("Content-Type", "text/html; charset=utf-8"); $response->end($cli->body); }); // 5. Æô¶¯HTTPЧÀÍÆ÷ $http->start();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀý´úÂ뽨ÉèÁËÒ»¸öSwoole HTTPЧÀÍÆ÷£¬¼àÌý¶Ë¿ÚºÅ9501¡£µ±ÓÐHTTPÇëÇóµÖ´ïʱ£¬Ð§ÀÍÆ÷½«·¢ËÍHTTPÇëÇóµ½°Ù¶ÈÍøÕ¾£¬²¢ÏìÓ¦HTMLÄÚÈÝ¡£
SwooleгÌHTTP¿Í»§¶Ë
SwooleÌṩÁË»ùÓÚг̵ÄHTTP¿Í»§¶Ë£¬Í¨¹ýг̿ÉÒÔÔÚµ¥¸öÀú³ÌÄÚÀïͬʱÌᳫ¶à¸öHTTPÇëÇ󣬲¢ÐÐÖ´ÐÐÇëÇ󣬶øÎÞÐ迪Æô¶à¸öÏ̻߳òÀú³Ì¡£
гÌHTTP¿Í»§¶ËµÄʹÓúÜÊǼòÆÓ£¬ÒÔÏÂÊÇÒ»¸öʹÓÃʾÀý£º
<?php // 1. ½¨ÉèгÌHTTP¿Í»§¶Ë $cli = new SwooleCoroutineHttpClient('www.www.zonelele.com', 80); // 2. ÉèÖÃÇëÇóÍ· $cli->setHeaders([ 'Host' => "www.www.zonelele.com", "User-Agent" => 'Chrome/49.0.2587.3', 'Accept' => 'text/html,application/xhtml+xml,application/xml', 'Accept-Encoding' => 'gzip', ]); // 3. ·¢ËÍHTTPÇëÇó $cli->get('/'); // 4. Êä³öÏìÓ¦ÄÚÈÝ echo $cli->body;
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀý´úÂ뽨ÉèÁËÒ»¸öгÌHTTP¿Í»§¶Ë£¬ÉèÖÃÇëÇóÍ·ºó·¢ËÍHTTPÇëÇ󣬲¢Êä³öÏìÓ¦ÄÚÈÝ¡£
ÔËÓÃгÌʵÏÖÅÀ³æÅÀÈ¡
ʹÓÃSwooleгÌHTTP¿Í»§¶Ë£¬ÎÒÃÇ¿ÉÒÔÇáËɵØʵÏÖ¸ßÐÔÄܵÄWebÅÀ³æ¡£ÒÔÏÂÊÇÒ»¸öʹÓÃгÌʵÏÖµÄÅÀ³æʾÀý£º
<?php // 1. ץȡ°Ù¶ÈËÑË÷Ч¹ûµÄÒ³Ãæ $html = file_get_contents('https://www.www.zonelele.com/s?ie=UTF-8&wd=swoole'); // 2. ÆÊÎöHTML£¬ÌáÈ¡ËÑË÷Ч¹ûÁбíµÄURL preg_match_all('/<a.*?href="(.*?)".*?>/is', $html, $matches); $urls = $matches[1]; // 3. ²¢·¢ÇëÇóËÑË÷Ч¹ûÁбíµÄURL $cli = new SwooleCoroutineHttpClient('www.www.zonelele.com', 80); foreach ($urls as $url) { $cli->setHeaders([ 'Host' => "www.www.zonelele.com", "User-Agent" => 'Chrome/49.0.2587.3', 'Accept' => 'text/html,application/xhtml+xml,application/xml', 'Accept-Encoding' => 'gzip', ]); $cli->get($url); echo $cli->body; } // 4. ¹Ø±ÕHTTP¿Í»§¶Ë $cli->close();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀý´úÂëÊ×ÏÈץȡ°Ù¶ÈËÑË÷¡°swoole¡±Òªº¦×ÖµÄÒ³Ã棬²¢ÆÊÎöHTML£¬ÌáÈ¡ËÑË÷Ч¹ûÁбíµÄURL£¬²¢²¢·¢ÇëÇóÕâЩURL¡£
×ܽá
Swoole×÷Ϊһ¸ö¸ßÐÔÄܵÄÍøÂçͨѶ¿ò¼Ü£¬ÆäгÌÌØÕ÷ΪWebÅÀ³æµÄ¿ª·¢ÌṩÁ˸ßЧµÄ½â¾ö¼Æ»®¡£Ê¹ÓÃSwooleгÌHTTP¿Í»§¶Ë£¬¿ÉÒÔ´ó·ùÌáÉýWebÅÀ³æµÄ²¢·¢ÇëÇóÄÜÁ¦£¬Í¬Ê±×èÖ¹¶àÏ̻߳ò¶àÀú³Ì´øÀ´µÄ×ÊÔ´ÏûºÄºÍÉÏÏÂÎÄÇл»¿ªÏú¡£
ÒÔÉϾÍÊÇSwoole½ø½×£ºÊ¹ÓÃг̾ÙÐÐWebÅÀ³æ¿ª·¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡