SwooleʵÏÖ¸ßÐÔÄܵÄRESTful APIЧÀÍÆ÷
Ëæ×Å»¥ÁªÍøµÄÒ»Ö±Éú³¤£¬Ô½À´Ô½¶àµÄ¹«Ë¾ºÍ×éÖ¯×îÏÈʹÓÃrestful apiÀ´ÌṩÊý¾ÝºÍӪҵЧÀÍ¡£restful apiÊÇÒ»ÖÖ¾«Á·Ã÷ÎúµÄapiÉè¼ÆÆøÑæÆøÑ棬Ëüͨ¹ýhttpÐÒé¾ÙÐÐͨѶ£¬Ê¹µÃ¿Í»§¶ËÓëЧÀͶËÖ®¼äµÄ½»»¥±äµÃºÜÊÇÇåÎúºÍÒ×ÓÚÃ÷È·¡£¶øswooleÔòÊÇÒ»¿î»ùÓÚphpÓïÑÔ¿ª·¢µÄ¸ßÐÔÄÜÍøÂçͨѶÒýÇ棬ͨ¹ýËü¿ÉÒÔʵÏÖ»ùÓÚhttpÐÒéµÄrestful apiЧÀÍÆ÷¡£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃSwooleʵÏÖ¸ßÐÔÄܵÄRESTful APIЧÀÍÆ÷¡£ÎÄÖн«°üÀ¨ÒÔÏÂÄÚÈÝ£º
ʲôÊÇRESTful API£¿
SwooleµÄ»ù±¾ÏÈÈÝ£»
SwooleʵÏÖRESTful APIЧÀÍÆ÷µÄÊÖÒÕÔÀí£»
SwooleʵÏÖRESTful APIЧÀÍÆ÷µÄ°ì·¨£»
¿ÉÒÔʹÓÃSwooleʵÏÖRESTful APIЧÀÍÆ÷µÄÏÖʵ°¸Àý¡£
Ò»¡¢Ê²Ã´ÊÇRESTful API£¿
RESTful APIÊÇÒ»ÖÖͨ¹ýHTTPÐÒé¾ÙÐÐͨѶµÄAPIÉè¼ÆÆøÑæÆøÑ棬Ëü°üÀ¨ÁËÒÔϼ¸¸öÌص㣺
ÌåÏÖ²ã״̬ת»¯£¨Representational State Transfer£¬¼ò³ÆREST£©£ºRESTful APIͨ¹ýHTTP¶¯´ÊʵÏÖÊý¾Ý×ÊÔ´µÄÔöɾ¸Ä²é²Ù×÷£¬²¢Í¨¹ýHTTP״̬Âë·µ»ØÇëÇóЧ¹û¡£
URIµØµã£ºURIÊÇRESTful APIµÄ×ÊԴ·¾¶£¬±êʶÁËЧÀͶËÒª»á¼ûµÄAPI×ÊÔ´¡£
HTTPÇëÇóºÍÏìÓ¦£ºRESTful APIʹÓÃHTTPÐÒé¾ÙÐÐÊý¾ÝµÄ´«Ê䣬¿Í»§¶ËÏòЧÀͶËÌᳫÇëÇó£¬Ð§ÀͶ˷µ»ØÏìÓ¦µÄÏìӦЧ¹û¡£
¶þ¡¢SwooleµÄ»ù±¾ÏÈÈÝ
SwooleÊÇÒ»¿î»ùÓÚPHPÓïÑÔ¿ª·¢µÄ¸ßÐÔÄÜÍøÂçͨѶÒýÇæ¡£Óë¹Å°åµÄPHPÓïÑÔÏà±È£¬Swoole¾ßÓÐÒÔϼ¸¸öÓŵ㣺
»ùÓÚÒì²½·ÇÛÕ±ÕµÄÍøÂç±à³ÌÄ£×Ó£»
Ö§³Ö¶àÀú³Ì¡¢Ð³ÌºÍÒì²½IOµÈÌØÕ÷£»
»ùÓÚPHPÓïÑÔ¿ª·¢£¬Ò×ÓÚÀ©Õ¹ºÍµ÷ÊÔ¡£
Òò´Ë£¬SwooleÔÚÍøÂç±à³Ì¡¢¸ß²¢·¢Ð§ÀÍÆ÷ºÍÂþÑÜʽϵͳµÈÁìÓòÓÐ×ÅÆÕ±éµÄÓ¦Óá£
Èý¡¢SwooleʵÏÖRESTful APIЧÀÍÆ÷µÄÊÖÒÕÔÀí
SwooleʵÏÖRESTful APIЧÀÍÆ÷µÄ½¹µãÊÖÒÕÊÇ»ùÓÚHTTPÐÒéµÄÍøÂçͨѶ¡£Swooleͨ¹ýHTTPÐÒéʵÏÖÁËÒÔϼ¸¸ö¹¦Ð§£º
ÎüÊÕ¿Í»§¶ËµÄHTTPÇëÇó£»
ÆÊÎöHTTPÇëÇóÖеÄURI¡¢ÇëÇóÒªÁìºÍÇëÇó²ÎÊýµÈÐÅÏ¢£»
´¦Àí¿Í»§¶ËµÄHTTPÇëÇ󣬲¢·µ»ØHTTPÏìӦЧ¹û¡£
±ðµÄ£¬Swoole»¹Ö§³ÖгÌÊÖÒÕ£¬¿ÉÒÔÔÚ²»½¨ÉèÏ̵߳ÄÇéÐÎÏÂʵÏÖ²¢·¢´¦Àí£¬´Ó¶øʵÏÖ¸ßÐÔÄܵÄRESTful APIЧÀÍÆ÷¡£
ËÄ¡¢SwooleʵÏÖRESTful APIЧÀÍÆ÷µÄ°ì·¨
×°ÖÃSwooleÀ©Õ¹
ÔÚPHPÇéÐÎÖÐ×°ÖÃSwooleÀ©Õ¹£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
pecl install swoole
µÇ¼ºó¸´ÖÆ
±àдRESTful APIЧÀÍÆ÷
ƾ֤RESTful APIµÄÉè¼ÆÔÔò£¬ÊµÏÖAPIµÄÔöɾ¸Ä²é²Ù×÷£¬Ê¾Àý´úÂëÈçÏ£º
$request_uri = $_SERVER['REQUEST_URI']; $request_method = $_SERVER['REQUEST_METHOD']; if ($request_method === 'GET') { // ´¦ÀíGETÇëÇó if ($request_uri === '/api/user') { // »ñÈ¡Óû§ÐÅÏ¢ // TODO } else { // »ñÈ¡ÆäËû×ÊÔ´ÐÅÏ¢ // TODO } } elseif ($request_method === 'POST') { // ´¦ÀíPOSTÇëÇó if ($request_uri === '/api/user') { // ½¨ÉèÓû§ÐÅÏ¢ // TODO } else { // ½¨ÉèÆäËû×ÊÔ´ÐÅÏ¢ // TODO } } elseif ($request_method === 'PUT') { // ´¦ÀíPUTÇëÇó if (preg_match('/^/api/user/d+$/', $request_uri)) { // ¸üÐÂÓû§ÐÅÏ¢ // TODO } else { // ¸üÐÂÆäËû×ÊÔ´ÐÅÏ¢ // TODO } } elseif ($request_method === 'DELETE') { // ´¦ÀíDELETEÇëÇó if (preg_match('/^/api/user/d+$/', $request_uri)) { // ɾ³ýÓû§ÐÅÏ¢ // TODO } else { // ɾ³ýÆäËû×ÊÔ´ÐÅÏ¢ // TODO } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËPHPµÄ»ù´¡Ó﷨ʵÏÖÁËÒ»¸öRESTful APIµÄЧÀÍÆ÷£¬Í¨¹ýÅжÏÇëÇóÒªÁìºÍÇëÇóµØµã£¬À´´¦Àí²î±ðµÄAPI²Ù×÷¡£
ʹÓÃSwooleÆô¶¯RESTful APIЧÀÍÆ÷
Æô¶¯RESTful APIЧÀÍÆ÷£¬¿ÉÒÔʹÓÃÒÔÏÂSwoole´úÂ룺
$server = new SwooleHttpServer('127.0.0.1', 9501); $server->on('Request', function ($request, $response) { $request_uri = $request->server['request_uri']; $request_method = $request->server['request_method']; if ($request_method === 'GET') { // ´¦ÀíGETÇëÇó if ($request_uri === '/api/user') { // »ñÈ¡Óû§ÐÅÏ¢ // TODO } else { // »ñÈ¡ÆäËû×ÊÔ´ÐÅÏ¢ // TODO } } elseif ($request_method === 'POST') { // ´¦ÀíPOSTÇëÇó if ($request_uri === '/api/user') { // ½¨ÉèÓû§ÐÅÏ¢ // TODO } else { // ½¨ÉèÆäËû×ÊÔ´ÐÅÏ¢ // TODO } } elseif ($request_method === 'PUT') { // ´¦ÀíPUTÇëÇó if (preg_match('/^/api/user/d+$/', $request_uri)) { // ¸üÐÂÓû§ÐÅÏ¢ // TODO } else { // ¸üÐÂÆäËû×ÊÔ´ÐÅÏ¢ // TODO } } elseif ($request_method === 'DELETE') { // ´¦ÀíDELETEÇëÇó if (preg_match('/^/api/user/d+$/', $request_uri)) { // ɾ³ýÓû§ÐÅÏ¢ // TODO } else { // ɾ³ýÆäËû×ÊÔ´ÐÅÏ¢ // TODO } } $response->end('Hello World'); }); $server->start();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëʵÏÖÁËÒ»¸ö¼òÆÓµÄRESTful APIЧÀÍÆ÷£¬ÆäÖÐon(‘Request’, function ($request, $response) {})¼àÌýÁËHTTPÇëÇ󣬲¢Ôڻص÷º¯ÊýÖд¦ÀíÁËÓªÒµÂß¼¡£
Îå¡¢¿ÉÒÔʹÓÃSwooleʵÏÖRESTful APIЧÀÍÆ÷µÄÏÖʵ°¸Àý
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʹÓÃSwooleʵÏÖRESTful APIЧÀÍÆ÷µÄʵÀý£º
$server = new SwooleHttpServer('0.0.0.0', 9501); $server->on('Request', function ($request, $response) { $method = $request->server['request_method']; $path = $request->server['path_info']; $params = $request->get ?? []; switch ($method) { case 'GET': if ($path == '/api/user') { // »ñÈ¡Óû§ÐÅÏ¢ $response->header("Content-Type", "application/json;charset=utf-8"); $response->end(json_encode($params)); // ¼ÙÉèÓû§ÐÅÏ¢´æ·ÅÔÚ$paramsÖÐ } else { // »ñÈ¡ÆäËû×ÊÔ´ÐÅÏ¢ $response->status(404); $response->end('Not Found'); } break; case 'POST': if ($path == '/api/user') { // ½¨ÉèÓû§ÐÅÏ¢ $response->status(201); $response->end('Create success'); } else { // ½¨ÉèÆäËû×ÊÔ´ÐÅÏ¢ $response->status(400); $response->end('Bad Request'); } break; case 'PUT': if (preg_match('/^/api/user/(d+)$/', $path, $matches)) { // ¸üÐÂÓû§ÐÅÏ¢ $id = $matches[1]; $response->status(200); $response->end("User $id updated"); } else { // ¸üÐÂÆäËû×ÊÔ´ÐÅÏ¢ $response->status(400); $response->end('Bad Request'); } break; case 'DELETE': if (preg_match('/^/api/user/(d+)$/', $path, $matches)) { // ɾ³ýÓû§ÐÅÏ¢ $id = $matches[1]; $response->status(204); $response->end(); } else { // ɾ³ýÆäËû×ÊÔ´ÐÅÏ¢ $response->status(400); $response->end('Bad Request'); } break; default: $response->status(405); $response->header("Allow", "GET,POST,PUT,DELETE"); $response->end('Method Not Allowed'); break; } }); $server->start();
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËSwooleµÄHTTPЧÀÍÆ÷£¬²¢ÔÚ¼àÌýµ½HTTPÇëÇóʱ£¬Í¨¹ýÅжϲî±ðµÄURIºÍÇëÇóÒªÁ죬À´´¦Àí²î±ðµÄAPI²Ù×÷¡£Í¨¹ýÕâÖÖ·½·¨£¬ÎÒÃǾͿÉÒÔʹÓÃSwooleʵÏÖ¸ßÐÔÄܵÄRESTful APIЧÀÍÆ÷ÁË¡£
½áÓï
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬¶ÁÕß¿ÉÒÔÏàʶµ½SwooleµÄ»ù±¾ÏÈÈÝ¡¢RESTful APIÉè¼ÆÔÀíÒÔ¼°Ê¹ÓÃSwooleÀ´ÊµÏÖRESTful APIЧÀÍÆ÷µÄÊÖÒÕÔÀíºÍ°ì·¨¡£Swoole¹ØÓÚ¸ßÐÔÄÜÍøÂçͨѶµÄÐèÇóºÜÊÇÊÊÓã¬Òò´ËËüÔÚÏÖʵ¹¤³ÌÁìÓòÓÐ×ÅÆÕ±éµÄÓ¦Óá£ÈôÊǶÁÕß¹ØÓÚSwooleµÄÃ÷È·»¹²»·óÉîÈ룬¿ÉÒÔ´Ó¹Ù·½µÄÎĵµÈëÊÖ£¬Í¨¹ýÒ»Ö±µÄѧϰºÍʵ¼ù£¬ÕÆÎÕSwooleµÄʹÓü¼ÇɺÍÓÅ»¯ÒªÁ죬´Ó¶øʵÏÖ¸üΪ¸ßЧµÄÍøÂçͨѶЧÀÍ¡£
ÒÔÉϾÍÊÇSwooleʵÏÖ¸ßÐÔÄܵÄRESTful APIЧÀÍÆ÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡