ʹÓÃSwoole·â×°¿É¿¿ÐԸߵÄÒì²½HTTP¿Í»§¶Ë
Ëæ×ÅwebÓ¦ÓóÌÐòµÄÒ»Ö±Éú³¤£¬http¿Í»§¶ËµÄÐèÇóÒ²Ô½À´Ô½Ö÷Òª£¬ÌØÊâÊÇÔÚ¿çÍøÂçÇëÇóÊý¾ÝµÄÇéÐÎÏ¡£ÔڹŰåµÄphpʵÏÖÖУ¬Ê¹ÓÃcurlµÈ¿â¾ÙÐÐÍøÂçÇëÇóÊÇÒ»ÖÖ³£¼ûµÄ·½·¨£¬µ«Æä²»Ö§³ÖÒì²½ÇëÇ󣬵¼ÖÂÔÚ´ó×Ú²¢·¢ÇëÇóʱÐÔÄÜÊܵ½ÏÞÖÆ¡£
SwooleÊÇÒ»¿î»ùÓÚPHPµÄгÌÍøÂçͨѶÒýÇ棬ÆäÌṩÁËÒ»ÖÖÒì²½ÇëÇóµÄ·½·¨£¬¿ÉÒÔ´ó´óÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSwoole·â×°Ò»¸ö¿É¿¿ÐԸߵÄÒì²½HTTP¿Í»§¶Ë¡£
ΪʲôʹÓÃSwooleÒì²½HTTP¿Í»§¶Ë
ÔÚ²¢·¢ÇëÇóʱ£¬Í¬²½µÄHTTP¿Í»§¶Ë»áÔì³ÉÐÔÄܵÄÆ¿¾±£¬¶øÒì²½µÄHTTP¿Í»§¶ËÔò¿ÉÒÔÔÚµ¥¸öÀú³ÌÖÐͬʱ´¦Àí¶à¸öÇëÇó¡£SwooleÌṩÁËÒ»ÖÖгÌÄ£×Ó£¬ÔÚ²»Çл»Àú³ÌµÄÇéÐÎÏÂʵÏÖÒì²½ÇëÇó£¬Í¬Ê±»¹ÄÜïÔÌÄÚ´æÕ¼ÓúÍTCPÅþÁ¬µÄ¿ªÏú£¬Ìá¸ßÓ¦ÓóÌÐòµÄÐÔÄÜ¡£
ʹÓÃSwooleÒì²½HTTP¿Í»§¶ËÉÐÓÐÒÔÏÂÀûÒ棺
Ìṩ¸»ºñµÄÊÂÎñ»Øµ÷º¯Êý£¬¿ÉÒÔÔÚÇëÇóÀֳɻòʧ°Üʱ¾ÙÐÐÏìÓ¦µÄ´¦Àí²Ù×÷£»
×Ô¶¯´¦ÀíHTTPÇëÇóµÄÇëÇóÍ·¡¢ÏìӦͷºÍÏìÓ¦ÄÚÈÝ£¬Àû±ã¿ª·¢ÕßʹÓã»
¼¯³ÉÁËÅþÁ¬³ØÖÎÀí£¬¿ÉÒÔÔڸ߲¢·¢ÏÂ×Ô¶¯ÖÎÀíTCPÅþÁ¬¡£
±àдÒì²½HTTP¿Í»§¶Ë
ÔÚ×îÏȱàдÒì²½HTTP¿Í»§¶ËÇ°£¬ÐèÒªÏÈ×°ÖÃSwooleÀ©Õ¹£¬²¢È·±£PHP°æ±¾Îª7.0ÒÔÉÏ¡£×°ÖÃÀ©Õ¹¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
pecl install swoole
µÇ¼ºó¸´ÖÆ
È»ºóÔÚPHPµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º
extension=swoole.so
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ×îÏȱàдÒì²½HTTP¿Í»§¶ËµÄ´úÂë¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒª½ç˵һ¸öHttpClientÀ࣬ÓÃÓÚÌᳫÒì²½HTTPÇëÇó¡£Ïêϸ´úÂëÈçÏ£º
class HttpClient { private $client; public function __construct($host, $port = 80, $ssl = false) { $scheme = $ssl ? 'https://' : 'http://'; $this->client = new SwooleCoroutineHttpClient($host, $port, $ssl); $this->client->setHeaders([ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Accept' => 'text/html,application/xhtml+xml,application/xml', 'Accept-Encoding' => 'gzip', ]); } public async function get($path, $params = []) { $path .= '?' . http_build_query($params); return $this->request('GET', $path); } public async function post($path, $params = []) { return $this->request('POST', $path, $params); } private async function request($method, $path, $params = []) { $this->client->setMethod($method); $this->client->setData($params); $this->client->execute($path); $statusCode = $this->client->statusCode; $body = $this->client->body; if ($statusCode !== 200) { throw new RuntimeException(sprintf('Request failed with status code %d', $statusCode)); } return $body; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇͨ¹ý__constructÒªÁì³õʼ»¯Ò»¸öSwooleµÄHttpClient¹¤¾ß¡£ÓÉÓÚÎÒÃÇÐèÒªÖ§³ÖHTTPºÍHTTPSÁ½ÖÖÐÒ飬ÒÔÊÇÎÒÃÇÐèÒªÅжÏURLµÄÐÒ飬ȻºóÉèÖöÔÓ¦µÄ¶Ë¿ÚºÅºÍSSLÑ¡Ïî¡£
È»ºóÎÒÃÇͨ¹ýgetºÍpostÒªÁìʵÏÖÁËÒì²½µÄHTTPÇëÇó¡£ÔÚÇëÇóÖУ¬ÎÒÃÇʹÓÃhttp_build_queryº¯Êý½«²ÎÊýתΪURLÐÎʽµÄ×Ö·û´®£¬²¢ÔÚURLÖÐÆ´½Ó²ÎÊý¡£È»ºóÎÒÃǽ«ÇëÇóÒªÁìºÍ²ÎÊýÉèÖÃÔÚ$client¹¤¾ßÖУ¬×îºóŲÓÃ$client->executeÒªÁìÌᳫÇëÇó¡£
ʹÓÃ$client->executeÒªÁìÌᳫÇëÇó£¬Swoole»á×Ô¶¯Ðµ÷ÇëÇóºÍÏìÓ¦£¬²¢ÆÚ´ýÏìÓ¦µÄµ½À´¡£ÔÚÏìÓ¦µÖ´ïºó£¬ÎÒÃÇ¿ÉÒÔ»ñÈ¡ÏìÓ¦µÄ״̬ÂëºÍÏìÓ¦Ì壬Ȼºóƾ֤״̬ÂëÀ´ÅжÏÇëÇóÊÇ·ñÀֳɡ£
ʹÓÃʾÀý
ΪÁËÑÝʾÔõÑùʹÓÃÉÏÊöµÄÒì²½HTTP¿Í»§¶Ë£¬ÎÒÃDZàдÁËÒ»¸ö¼òÆÓµÄʾÀý¡£´úÂëÈçÏ£º
$client = new HttpClient('httpbin.org'); go(function () use ($client) { $result = yield $client->get('/get', ['hello' => 'world']); var_dump($result); }); go(function () use ($client) { $result = yield $client->post('/post', ['name' => 'Jack', 'age' => 29]); var_dump($result); });
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×Ïȳõʼ»¯ÁËÒ»¸öHttpClient¹¤¾ß£¬²¢ÉèÖÃÔ¶³ÌÖ÷»úµØµã¡£È»ºóÎÒÃÇͨ¹ýgoгÌÌᳫÁËÁ½¸öÒì²½ÇëÇó£¬Ò»¸öÊÇGETÇëÇó£¬ÁíÒ»¸öÊÇPOSTÇëÇ󣬻®·Ö´øÓвÎÊý[‘hello’ => ‘world’]ºÍ[‘name’ => ‘Jack’, ‘age’ => 29]¡£ÔÚÿ¸öÒì²½ÇëÇóÍê³Éºó£¬ÎÒÃÇͨ¹ývar_dumpÊä³öÏìÓ¦ÌåÄÚÈÝ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃSwoole·â×°Ò»¸ö¿É¿¿ÐԸߵÄÒì²½HTTP¿Í»§¶Ë£¬ÒÔÖ§³Ö²¢·¢ÇëÇó¡£Í¨¹ýʹÓÃSwoole£¬ÎÒÃÇ¿ÉÒÔÇáËɵØʵÏÖгÌÒì²½ÇëÇó£¬×èÖ¹¹Å°åPHPÓ¦ÓóÌÐòÔÚ²¢·¢ÇëÇóʱµÄÐÔÄÜÆ¿¾±¡£
ÒÔÉϾÍÊÇʹÓÃSwoole·â×°¿É¿¿ÐԸߵÄÒì²½HTTP¿Í»§¶ËµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡