尊龙凯时人生就是博

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Ôõô×öС³ÌÐò½Ó¿ÚµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ