×ðÁú¿­Ê±

thinkphp5дapi½Ó¿ÚÇëÇó·µ»Ø

Ëæ×Å»¥ÁªÍøµÄÆÕ¼° £¬web¿ª·¢µÄÐèÇóÒ²Ô½À´Ô½¶à¡£ÔÚweb¿ª·¢ÖÐ £¬api½Ó¿Ú³ÉÁ˲»¿É»òȱµÄÒ»²¿·Ö £¬¶øÔÚphpÁìÓò £¬thinkphp5ÎÞÒÉÊÇ×îÊ¢ÐеĿò¼ÜÖ®Ò»¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp5¿ò¼ÜÀ´±àдapi½Ó¿ÚÇëÇóºÍ·µ»Ø¡£

Ò»¡¢API½Ó¿ÚÇëÇó

1.1 GETÇëÇó

ÔÚthinkphp5ÖÐ £¬Ê¹ÓÃGETÇëÇó·¢ËÍAPIÇëÇóºÜÊǼòÆÓ¡£ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺

use thinkRequest;

$request = Request::instance();

$name = $request->get('name');

$age = $request->get('age');

//×ö³öÏìÓ¦

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖÐ £¬ÎÒÃÇʹÓÃÁËRequestÀàµÄʵÀý»¯¹¤¾ß £¬È»ºóʹÓÃgetÒªÁì»ñÈ¡ÇëÇó²ÎÊý¡£½ÓÏÂÀ´¿ÉÒÔ¶Ô²ÎÊý¾ÙÐÐÏìÓ¦¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡± £»

1.2 POSTÇëÇó

ÔÚthinkphp5ÖÐ £¬Ê¹ÓÃPOSTÇëÇó·¢ËÍAPIÇëÇóµÄÒªÁìºÍGETÇëÇó»ùÄÚÇéͬ¡£ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺

use thinkRequest;

$request = Request::instance();

$name = $request->post('name');

$age = $request->post('age');

//×ö³öÏìÓ¦

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖÐ £¬ÎÒÃÇͬÑùʹÓÃÁËRequestÀàµÄʵÀý»¯¹¤¾ß £¬È»ºóʹÓÃpostÒªÁì»ñÈ¡ÇëÇó²ÎÊý¡£½ÓÏÂÀ´¿ÉÒÔ¶Ô²ÎÊý¾ÙÐÐÏìÓ¦¡£

1.3 JSONÇëÇó

ÔÚAPI¿ª·¢ÖÐ £¬JSONÇëÇóÒ²ÊǷǾ­³£¼ûµÄÒ»ÖÖ·½·¨ £¬³ýÁËGETºÍPOSTÇëÇó £¬thinkphp5»¹ÌṩÁ˽ÓÊܺÍÏìÓ¦JSONÊý¾ÝµÄÒªÁì¡£ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺

use thinkRequest;

$request = Request::instance();

$data = $request->getContent();

$json = json_decode($data, true);

$name = $json['name'];

$age = $json['age'];

//×ö³öÏìÓ¦

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖÐ £¬ÎÒÃÇÊ×ÏÈʹÓÃgetContentÒªÁì»ñÈ¡ÇëÇóÌåÖеÄJSONÊý¾Ý £¬È»ºóʹÓÃjson_decode½«JSON×Ö·û´®×ª»¯ÎªÊý×é¡£½ÓÏÂÀ´¶ÔÊý×é¾ÙÐвÙ×÷»òÏìÓ¦¡£

¶þ¡¢API½Ó¿Ú·µ»Ø

ÔÚ¿ª·¢API½Ó¿Úʱ £¬·µ»ØÊý¾ÝͬÑùºÜÊÇÖ÷Òª¡£ÎÒÃÇͨ³£ÐèÒª·µ»ØjsonÊý¾Ý £¬ÒÔÏÂÊÇÒ»¸öʾÀý´úÂ룺

use thinkController;

class ApiController extends Controller {

    public function index() {

        //Êý¾ÝÊý×é

        $result = [
            'code' => 0,
            'msg' => 'success',
            'data' => [
                'name' => 'test',
                'age' => 18
            ]
        ];

        //·µ»ØjsonÊý¾Ý

        return json($result);

    }

}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖÐ £¬ÎÒÃÇÊ×ÏȽç˵·µ»ØÊý¾ÝµÄÊý×é £¬È»ºóʹÓÃjsonº¯Êý½«Êý×éת»¯Îªjson×Ö·û´®·µ»Ø¡£

Èý¡¢×ܽá

±¾ÎÄÖÐ £¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃthinkphp5¿ò¼ÜÀ´±àдAPI½Ó¿ÚÇëÇóºÍÏìÓ¦¡£ÔÚÏÖʵ¿ª·¢ÖÐ £¬ÎÒÃÇ»¹ÐèҪעÖؽӿÚÇëÇóµÄÇå¾²ÐÔ¡¢Êý¾ÝÃûÌõÈÎÊÌ⡣ϣÍû±¾ÎÄÄܹ»¶Ô¶ÁÕßÓÐËù×ÊÖú £¬Îª¸÷È˵ÄAPI¿ª·¢ÌṩһЩ×ÊÖú¡£

ÒÔÉϾÍÊÇthinkphp5дapi½Ó¿ÚÇëÇ󷵻صÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

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

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

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