ThinkPHP¿ò¼ÜÖзâ×°µÄPostÇëÇó¹¦Ð§
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬webÓ¦ÓóÌÐòÖð½¥³ÉΪÈËÃÇÉúÑÄÖв»¿É»òȱµÄÒ»²¿·Ö¡£ÔÚÕâ¸öÅä¾°Ï£¬phpÓïÑÔ³ÉΪ×îÊ¢ÐеÄweb¿ª·¢ÓïÑÔÖ®Ò»¡£¶ø¿ª·¢¿ò¼ÜÔòÊÇphp webÓ¦ÓóÌÐòµÄÖ÷Òª¹¤¾ß¡£ÆäÖУ¬thinkphpÊÇÒ»¸ö¹¦Ð§Ç¿Ê¢¡¢Ò×ÓÚʹÓõÄphp¿ª·¢¿ò¼Ü£¬¹ãÊÜ¿ª·¢ÕßÇàíù¡£±¾ÎĽ«ÏÈÈÝthinkphp¿ò¼ÜÖзâ×°µÄpostÇëÇó¹¦Ð§¡£
Ò». PostÇëÇóµÄ¿´·¨
ÔÚWebÓ¦ÓóÌÐòÖУ¬HTTPÇëÇóÊÇÎÒÃÇ×î³£ÓõĽ»»¥·½·¨¡£ÆäÖУ¬POSTÇëÇóºÍGETÇëÇóÊÇ×îΪ³£¼ûµÄÁ½ÖÖÇëÇó·½·¨¡£¼òÆÓÀ´Ëµ£¬POSTÇëÇó¾ÍÊǽ«Êý¾ÝÌá½»µ½Ð§ÀÍÆ÷¶Ë£¬²¢ÆÚ´ýЧÀÍÆ÷ÏìÓ¦¡£ÓëGETÇëÇó²î±ðµÄÊÇ£¬POSTÇëÇóÖÐÌá½»µÄÊý¾Ý²»»á·ºÆðÔÚURLÖС£
ÔÚPHPÓïÑÔÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃ$_POSTÈ«¾Ö±äÁ¿À´»ñµÃPOSTÇëÇóÖÐÌá½»µÄÊý¾Ý¡£ÀýÈ磺
$username = $_POST['username']; $password = $_POST['password'];
µÇ¼ºó¸´ÖÆ
¶þ. ThinkPHP·â×°µÄPostÇëÇó¹¦Ð§
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÄÚÖõÄRequestÀàÀ´ÊµÏÖPOSTÇëÇó¡£ÏêϸÀ´Ëµ£¬¿ÉÒÔͨ¹ýʵÀý»¯RequestÀàµÄ·½·¨£¬Å²ÓÃpostÒªÁìÀ´¾ÙÐÐPOSTÇëÇó¡£ÀýÈ磺
use think\facade\Request; // ·¢ËÍPOSTÇëÇó $response = Request::post('http://example.com', ['username' => 'admin', 'password' => '123456']);
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃǽ«»áÏòhttp://example.comÕâ¸öµØµã·¢ËÍÒ»¸öPOSTÇëÇ󣬲¢Ìá½»Ò»¸ö°üÀ¨Óû§ÃûºÍÃÜÂëµÄ¹ØÁªÊý×é¡£·¢ËÍÇëÇóÖ®ºó£¬Ð§ÀÍÆ÷»á·µ»ØÒ»¸öÏìӦЧ¹û¡£ÎÒÃÇ¿ÉÒÔ½«Õâ¸öЧ¹ûÉúÑÄÔÚ$response±äÁ¿ÖУ¬²¢ÔÚÐèÒªµÄʱ¼ä¾ÙÐд¦Öóͷ£¡£ÐèҪעÖصÄÊÇ£¬ÈôÊÇÇëÇó·ºÆð¹ýʧ£¬Ò²ÐèÒª¾ÙÐÐÒì³£´¦Öóͷ£¡£ÀýÈ磺
use think\exception\HttpException; use think\facade\Request; try { // ·¢ËÍPOSTÇëÇó $response = Request::post('http://example.com', ['username' => 'admin', 'password' => '123456']); } catch (HttpException $e) { // ±¬·¢Òì³££¬·µ»Ø¹ýʧÐÅÏ¢ return $e->getMessage(); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬ÎÒÃÇʹÓÃÁËtry-catchÓï¾äÀ´²¶»ñHttpExceptionÒì³£¡£ÈôÊDZ¬·¢Òì³££¬ÎÒÃǻὫÒì³£ÐÅÏ¢×÷ΪЧ¹û·µ»Ø¡£
Èý. ½øÒ»²½ÏàʶThinkPHP¿ò¼ÜÖеÄPostÇëÇó¹¦Ð§
³ýÁËRequestÀ࣬ThinkPHP¿ò¼ÜÖÐÉÐÓÐÐí¶àÆäËûµÄÀàºÍº¯Êý¿ÉÒÔÓÃÀ´ÊµÏÖPOSTÇëÇó¡£ÔÚÕâÀÎÒÃÇÏÈÈÝһЩ³£ÓõÄÀàºÍº¯Êý¡£
HttpÀà
ÔÚThinkPHP¿ò¼ÜÖУ¬HttpÀà¿ÉÒÔÓÃÀ´·¢ËÍHTTPÇëÇ󣬲¢»ñÈ¡ÏìӦЧ¹û¡£Í¨¹ýʵÀý»¯HttpÀàµÄ·½·¨£¬¿ÉÒÔŲÓÃpostÒªÁìÀ´·¢ËÍPOSTÇëÇó¡£ÀýÈ磺
use think\facade\Http; // ·¢ËÍPOSTÇëÇó $response = Http::post('http://example.com', ['username' => 'admin', 'password' => '123456']);
µÇ¼ºó¸´ÖÆ
ÔÚ·¢ËÍPOSTÇëÇóÖ®ºó£¬HttpÀà»á·µ»ØÒ»¸öResponse¹¤¾ß£¬ÆäÖаüÀ¨ÏìÓ¦ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔͨ¹ýŲÓÃResponse¹¤¾ßµÄgetContentÒªÁìÀ´»ñÈ¡ÏìӦЧ¹û¡£ÀýÈ磺
use think\facade\Http; // ·¢ËÍPOSTÇëÇó $response = Http::post('http://example.com', ['username' => 'admin', 'password' => '123456']); // »ñÈ¡ÏìӦЧ¹û $content = $response->getContent();
µÇ¼ºó¸´ÖÆ
CurlÀà
ÔÚPHPÓïÑÔÖУ¬CurlÀ©Õ¹ÊÇÓÃÀ´ÊµÏÖHTTPÇëÇóµÄÖ÷Òª×é¼þ¡£ÔÚThinkPHP¿ò¼ÜÖУ¬CurlÀàÊǶÔCurlÀ©Õ¹¾ÙÐзâ×°ºóµÄÒ»¸öÀ࣬¿ÉÒÔÀû±ãµØʵÏÖHTTPÇëÇó¡£ Í¨¹ýʵÀý»¯CurlÀàµÄ·½·¨£¬¿ÉÒÔŲÓÃpostÒªÁìÀ´·¢ËÍPOSTÇëÇó¡£ÀýÈ磺
use think\facade\Curl; // ·¢ËÍPOSTÇëÇó $response = Curl::post('http://example.com', ['username' => 'admin', 'password' => '123456']);
µÇ¼ºó¸´ÖÆ
±¬·¢POSTÇëÇóÖ®ºó£¬CurlÀàÒ²»á·µ»ØÒ»¸öResponse¹¤¾ß£¬ÆäÖаüÀ¨ÏìÓ¦ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔͨ¹ýŲÓÃResponse¹¤¾ßµÄgetContentÒªÁìÀ´»ñÈ¡ÏìӦЧ¹û¡£ÀýÈ磺
use think\facade\Curl; // ·¢ËÍPOSTÇëÇó $response = Curl::post('http://example.com', ['username' => 'admin', 'password' => '123456']); // »ñÈ¡ÏìӦЧ¹û $content = $response->getContent();
µÇ¼ºó¸´ÖÆ
RequestÀàÖеÄinputÒªÁì
³ýÁËpostÒªÁ죬ThinkPHPÖеÄRequestÀ໹ÌṩÁËinputÒªÁìÀ´»ñÈ¡POSTÇëÇóÖÐÌá½»µÄÊý¾Ý¡£ÀýÈ磺
use think\facade\Request; // »ñÈ¡POSTÇëÇóÖÐÌá½»µÄusername²ÎÊý $username = Request::input('username');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÀý×ÓÖУ¬ÎÒÃÇͨ¹ýinputÒªÁì»ñÈ¡ÁËPOSTÇëÇóÖÐÌá½»µÄusername²ÎÊý¡£ÐèҪעÖصÄÊÇ£¬ÈôÊÇPOSTÇëÇóÖÐûÓÐÌá½»¸Ã²ÎÊý£¬$username±äÁ¿½«»áΪnull¡£
ËÄ. ×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËPOSTÇëÇóµÄ¿´·¨£¬²¢ÇÒÏêϸ½â˵ÁËThinkPHP¿ò¼ÜÖзâ×°µÄPOSTÇëÇó¹¦Ð§¡£³ýÁËRequest¡¢Http¡¢CurlÀàÒÔÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÆäËûÒªÁìÀ´ÊµÏÖPOSTÇëÇó¡£×ÜÖ®£¬ÔÚPHP WebÓ¦ÓóÌÐò¿ª·¢ÖУ¬POSTÇëÇóÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ½»»¥·½·¨£¬Ïà֪Ȥ¹ØÊÖÒÕÊÇ¿ª·¢Ö°Ô±µÄ±Ø±¸ÊÖÒÕ¡£
ÒÔÉϾÍÊÇThinkPHP¿ò¼ÜÖзâ×°µÄPostÇëÇó¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡