thinkphpÔõô×öС³ÌÐò½Ó¿Ú
Ëæ×ÅÒƶ¯»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬Ð¡³ÌÐò³ÉΪÁËÒ»ÖÖºÜÊÇÊ¢ÐеÄÒƶ¯Ó¦ÓóÌÐò¡£¶øÓë´Ëͬʱ£¬×÷Ϊһ¿îʹÓÃÆÕ±éµÄphp¿ò¼Ü£¬thinkphpÒ²»ñµÃÁË¿í´ó¿ª·¢ÕßµÄÇàíù¡£ÄÇô£¬ÔõÑùÔÚʹÓÃthinkphpµÄͬʱ£¬ÎªÐ¡³ÌÐòÌṩ½Ó¿ÚÄØ£¿
Ò»¡¢Ë¼Ð÷ÆÊÎö
ÔÚ×îÏÈ֮ǰ£¬ÎÒÃÇÏÈÀ´ÆÊÎöÒ»ÏÂÔõÑùͨ¹ýthinkphpÀ´ÎªÐ¡³ÌÐòÌṩ½Ó¿Ú¡£
½Ó¿ÚµÄ½ç˵£ºÐ¡³ÌÐò½Ó¿ÚÊÇÒ»¸öÖ§³Öpost¡¢getµÈÇëÇó·½·¨µÄAPI£¬¿ÉÒÔÓëС³ÌÐò¾ÙÐÐÊý¾Ý½»»¥£¬Ò»Ñùƽ³£·µ»ØµÄÊÇJSON»òXMLÊý¾ÝÃûÌá£
JSONÃûÌõÄÊý¾Ý£ºÐ¡³ÌÐò½Ó¿Ú·µ»ØµÄÊý¾ÝÃûÌÃÖ÷ÒªÊÇJSONÃûÌã¬ÐèҪʹÓÃthinkphpµÄJSON()º¯ÊýÀ´¾ÙÐÐÃûÌû¯Êä³ö¡£
½Ó¿ÚµÄ·ÓÉ£ºthinkphpµÄ·Óɹ¦Ð§ºÜÊÇÇ¿Ê¢£¬¿ÉÒÔÎÞаµØΪÖÖÖÖÇëÇóÉèÖòî±ðµÄ·Óɵص㡣
Êý¾Ý¿âµÄ²Ù×÷£º½Ó¿ÚÐèҪʹÓõ½Êý¾Ý¿â²Ù×÷£¬Òò´ËÐèҪʹÓÃthinkphpÌṩµÄÊý¾Ý¿â²Ù×÷Àà¡£
¶þ¡¢½Ó¿ÚµÄ½ç˵
½Ó¿ÚÊÇÖ¸ÔÚ¿Í»§¶ËºÍЧÀÍÆ÷Ö®¼ä¾ÙÐÐÊý¾Ý´«ÊäµÄAPI£¬Ëü¿ÉÒÔÖ§³Öpost¡¢getµÈÇëÇó·½·¨£¬Ò»Ñùƽ³£·µ»ØµÄÊÇJSON»òXMLÊý¾ÝÃûÌá£Òò´Ë£¬ÎÒÃÇÐèÒª½ç˵һ¸öAPI½Ó¿ÚÀ´ÎªÐ¡³ÌÐòÌṩÊý¾Ý½»»¥¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Ê×ÏÈ£¬ÔÚthinkphpµÄapplicationĿ¼Ï£¬Ð½¨Ò»¸öapiĿ¼¡£
ÔÚapiĿ¼Ï£¬Ð½¨Ò»¸öcontrollerĿ¼£¬²¢ÔÚ¸ÃĿ¼ÏÂн¨Ò»¸öIndex.php¿ØÖÆÆ÷¡£
ÔÚ¿ØÖÆÆ÷Öнç˵һ¸öÃûΪapiµÄ²Ù×÷ÒªÁ죬¸ÃÒªÁìÎüÊÕÒ»¸ö²ÎÊý£¬ÓÃÓÚÖ¸¶¨ÐèÒª»ñÈ¡µÄÊý¾ÝÀàÐÍ¡£ÀýÈ磺
public function api($type){ if($type == 'news'){ //»ñÈ¡×îÐÂÐÂÎÅÊý¾Ý }elseif($type == 'weather'){ //»ñÈ¡ÌìÆøÊý¾Ý }else{ //ÆäËûÊý¾Ý } }
µÇ¼ºó¸´ÖÆ
ÔÚ¸ÃÒªÁìÖÐʵÏÖÊý¾ÝµÄ»ñÈ¡¡£
×îºó£¬ÐèҪʹÓÃJSON()º¯Êý½«Êý¾ÝÃûÌû¯ÎªJSONÃûÌ㬲¢Í¨¹ýechoÊä³ö¡£
public function api($type){ if($type == 'news'){ //»ñÈ¡×îÐÂÐÂÎÅÊý¾Ý $data = array( 'title' => '½ñÈÕÒªÎÅ', 'content' => 'xxx' ); }elseif($type == 'weather'){ //»ñÈ¡ÌìÆøÊý¾Ý $data = array( 'city' => '±±¾©', 'weather' => 'ÇçÌì' ); }else{ //ÆäËûÊý¾Ý $data = array( 'result' => 'error', 'message' => '²ÎÊý¹ýʧ' ); } echo json_encode($data); }
µÇ¼ºó¸´ÖÆ
Èý¡¢½Ó¿ÚµÄ·ÓÉ
ÔÚthinkphpÖУ¬Â·Óɹ¦Ð§ºÜÊÇÇ¿Ê¢£¬¿ÉÒÔÎÞаµØΪÖÖÖÖÇëÇóÉèÖòî±ðµÄ·Óɵص㡣ÔÚС³ÌÐò½Ó¿ÚµÄʵÏÖÀú³ÌÖУ¬ÎÒÃÇÐèҪʹÓ÷ÓÉÀ´½«Óû§µÄÇëÇóת·¢µ½ÏìÓ¦µÄ½Ó¿Ú²Ù×÷ÒªÁì¡£
ÔÚconfigĿ¼µÄroute.phpÎļþÖУ¬¿ÉÒÔÉèÖÃÏìÓ¦µÄ·ÓɹæÔò¡£
return [ //С³ÌÐò½Ó¿Ú·ÓÉ 'apidata/:type' => 'api/Index/api' ];
µÇ¼ºó¸´ÖÆ
ÉÏÊö·ÓɹæÔòÖУ¬ÐèҪעÖصÄÊÇ:typeÊÇÒ»¸öռλ·û£¬Ëü¿ÉÒÔÆ¥ÅäÈκÎ×Ö·û£¬ÀýÈçnews¡¢weather£¬µÈµÈ¡£
×îºó£¬µ±Óû§»á¼ûÖ¸¶¨µÄ·ÓÉʱ£¬ÏµÍ³»á×Ô¶¯½«:typeÖеÄÖµ´«Èë¿ØÖÆÆ÷µÄapiÒªÁìÖУ¬ÀýÈ磺
http://www.example.com/apidata/news
µÇ¼ºó¸´ÖÆ
ËÄ¡¢Êý¾Ý¿âµÄ²Ù×÷
ͨ³£ÇéÐÎÏ£¬Ð¡³ÌÐòµÄ½Ó¿ÚÐèҪʹÓõ½Êý¾Ý¿â²Ù×÷À´»ñÈ¡Êý¾Ý¡£ÔÚthinkphpÖУ¬¿ÉÒÔʹÓÃϵͳÌṩµÄÊý¾Ý¿â²Ù×÷ÀàÀ´ÊµÏÖÊý¾ÝµÄÔö¡¢É¾¡¢¸Ä¡¢²éµÈ²Ù×÷¡£
Ê×ÏÈ£¬ÐèÒªÉèÖÃÊý¾Ý¿â²ÎÊý£¬ÔÚconfigĿ¼ÖеÄdatabase.phpÎļþÖоÙÐÐÉèÖá£
return [ // Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // ЧÀÍÆ÷µØµã 'hostname' => 'localhost', // Êý¾Ý¿âÃû 'database' => 'dbname', // Óû§Ãû 'username' => 'dbuser', // ÃÜÂë 'password' => 'dbpass', // ¶Ë¿Ú 'hostport' => '', // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8 'charset' => 'utf8', // Êý¾Ý¿â±íǰ׺ 'prefix' => '', ];
µÇ¼ºó¸´ÖÆ
ÔÚ¿ØÖÆÆ÷ÖУ¬Ê¹ÓÃÊý¾Ý¿â²Ù×÷Àà¾ÙÐÐÊý¾ÝÅÌÎÊ»ò¸üвÙ×÷¡£
public function api($type){ if($type == 'news'){ //»ñÈ¡×îÐÂÐÂÎÅÊý¾Ý $news = Db::table('news')->order('publish_time desc')->limit(10)->select(); $data = array( 'title' => '½ñÈÕÒªÎÅ', 'list' => $news ); }elseif($type == 'weather'){ //»ñÈ¡ÌìÆøÊý¾Ý $data = array( 'city' => '±±¾©', 'weather' => 'ÇçÌì' ); }else{ //ÆäËûÊý¾Ý $data = array( 'result' => 'error', 'message' => '²ÎÊý¹ýʧ' ); } echo json_encode($data); }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬Ê¹ÓÃÁËDb::table()º¯Êý»ñÈ¡Ò»¸öÊý¾Ý±íµÄ²Ù×÷¹¤¾ß£¬²¢Í¨¹ýorder()¡¢limit()µÈº¯Êý¾ÙÐÐÊý¾ÝÅÌÎʲÙ×÷¡£
×ܽ᣺ͨ¹ýÉÏÊö°ì·¨£¬ÎÒÃÇ¿ÉÒÔºÜÊǼòÆÓµØΪС³ÌÐòÌṩ½Ó¿Ú£¬ÊµÏÖÊý¾ÝµÄ»ñÈ¡ºÍ½»»¥¡£ÔÚÏÖʵ¿ª·¢Àú³ÌÖУ¬¿ÉÒÔƾ֤ÏêϸµÄÐèÇó¾ÙÐÐÊʵ±µÄÐ޸ĺÍÀ©Õ¹£¬ÒÔÖª×ã²î±ðµÄÓ¦Óó¡¾°¡£Í¬Ê±£¬Ò²½¨Ò鿪·¢ÕßÃǶàÏàʶthinkphpµÄÏà¹ØÊÖÒÕºÍ֪ʶ£¬ÒÔ±ã¸üºÃµØÊ©Õ¹ÆäÓÅÊÆ£¬ÌáÉý¿ª·¢Ð§ÂÊ¡£
ÒÔÉϾÍÊÇthinkphpÔõô×öС³ÌÐò½Ó¿ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡