SwooleʵÏָ߲¢·¢»á¼ûµÄwget
ÔÚ»¥ÁªÍøʱ´ú£¬¾³£»áÓöµ½ÐèÒª»á¼û´ó×ÚurlµÄ³¡¾°£¬ÈçÅÀ³æ¡¢Êý¾ÝÊÕÂ޵ȡ£¹Å°åµÄwget»òcurl¹¤¾ßÔÚ¾ÙÐи߲¢·¢»á¼ûʱ£¬ºÜÈÝÒ×·ºÆðÆ¿¾±ÒÔ¼°ÐÔÄÜÎÊÌâ¡£¶øswoole£¬×÷ΪphpµÄÀ©Õ¹Ä£¿é£¬¿ÉÒÔΪÎÒÃÇÌṩһ¸ö¸ßЧµÄÌæ»»¼Æ»®¡£
SwooleÊÇÒ»¸ö¿ªÔ´µÄPHPÀ©Õ¹£¬Æä×î³õµÄÉè¼ÆÄ¿µÄÊÇÓÃÓÚ¹¹½¨¸ßÐÔÄÜ¡¢¸ß¿ÉÉìËõÐÔµÄÍøÂçЧÀÍÆ÷ºÍWebЧÀÍ¡£Ëæ×ÅÆäÒ»Ö±µÄÍêÉƺÍÉú³¤£¬SwooleÏÖÔÚÒѾ³ÉΪÁËPHP¿ª·¢ÖÐÖ÷ÒªµÄÒ»²¿·Ö£¬ÎªÎÒÃÇÌṩÁËÐí¶àÓÅÒìµÄÌØÕ÷ºÍ¹¤¾ß¡£ÆäÖУ¬ÆäÌṩµÄÒì²½·ÇÛÕ±ÕI/OÄ£×Ó£¬Ê¹µÃÔڸ߲¢·¢»á¼û³¡¾°Ï£¬PHPÓ¦ÓñÈÒÔÍùÔ½·¢¸ßЧ¡£
ÄÇô£¬ÔõÑùʹÓÃSwooleʵÏָ߲¢·¢»á¼ûµÄwget¹¤¾ßÄØ£¿ÎÒÃÇ¿ÉÒÔÏȼòÆÓÏàʶÒÔÍù¹Å°åwget¹¤¾ßµÄÔÀí£ºwgetÌᳫһ´ÎHTTPÇëÇó£¬È»ºóÆÚ´ýЧÀÍÆ÷ÏìÓ¦¿¢Êºó²Å¾ÙÐÐÏÂÒ»´ÎÇëÇó¡£ÕâÖÖģʽÎÞÒÉ»áÓÉÓÚÆÚ´ýÏìÓ¦¶øµ¼Ö²¢·¢ÐÔÄÜ×ßÏÂÆ·¡£
¶øÔÚSwooleÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃг̵ÄÌص㣬½«Ã¿´ÎHTTPÇëÇóת»¯ÎªÒ»¸öг̣¬Ð³ÌÔÚÆÚ´ýÏìÓ¦µÄÀú³ÌÖпÉÒÔ±»¹ÒÆ𣬲»Õ¼ÓÃϵͳ×ÊÔ´£¬ÔÚÏìÓ¦¿¢ÊºóÔÙ»Ö¸´Ö´ÐС£ÕâÑù¿ÉÒÔʵÏָ߲¢·¢µÄHTTPÇëÇ󣬲¢ÇÒÔÚÐÔÄܺÍ×ÊÔ´Õ¼Ó÷½Ã涼¾ßÓÐÓÅÊÆ¡£
ÒÔÏÂÊÇÒ»¶ÎʹÓÃSwooleʵÏָ߲¢·¢»á¼ûµÄwget¹¤¾ßµÄʾÀý´úÂ룺
<?php $client = new SwooleCoroutineHttpClient('www.example.com', 80); go(function () use ($client) { $client->get('/url_1'); echo $client->body; }); go(function () use ($client) { $client->get('/url_2'); echo $client->body; }); go(function () use ($client) { $client->get('/url_3'); echo $client->body; }); $client->close(); ?>
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËSwooleÌṩµÄCoroutineHttpClientÀ࣬ͨ¹ýŲÓÃgetÒªÁìÌᳫHTTPÇëÇó¡£ÎÒÃÇͬʱ¿ªÆôÁËÈý¸öг̣¬»®·Ö¾ÙÐÐÁ˲î±ðURLµÄÇëÇ󣬲¢ÔÚ»ñÈ¡µ½ÏìÓ¦ºóʹÓÃechoÓï¾äÊä³öÏìÓ¦µÄÄÚÈÝ¡£ÔÚ×îºó£¬ÎÒÃÇʹÓÃÁËcloseÒªÁì¹Ø±ÕÁË¿Í»§¶ËÅþÁ¬¡£
»ùÓÚSwooleµÄ¸ß²¢·¢»á¼ûµÄwget¹¤¾ß£¬¿ÉÒÔ´ó´óÌáÉýÎÒÃÇÔÚURL»á¼û·½ÃæµÄЧÂʺÍÐÔÄÜ¡£Ïà±ÈÓڹŰåµÄwgetÒÔ¼°curl¹¤¾ß£¬Æä´¦Öóͷ£ÄÜÁ¦ºÍ²¢·¢ÐÔÄܶ¼¾ßÓÐÓÅÊÆ£¬ÇÒ¹ØÓÚPHPÓ¦ÓÃÖд¦Öóͷ£¸ß²¢·¢ÇëÇóµÄÓªÒµ³¡¾°£¬Ò²¾ßÓкÜÇ¿µÄÊÊÓÃÐÔ¡£
ÒÔÉϾÍÊÇSwooleʵÏָ߲¢·¢»á¼ûµÄwgetµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡