thinkphp»ñÈ¡²»µ½postÊý¾ÝÔõÑù½â¾ö
Ò»¡¢ÎÊÌâÕ÷Ïó
Ìá½»±íµ¥ºó£¬Í¨¹ý request->param() »ò $this->request->param() »ñÈ¡²»µ½ post Êý¾Ý£¬»ñµÃµÄÊÇ¿ÕÊý×é¡£
¶þ¡¢ÎÊÌâÔµ¹ÊÔÓÉ
±íµ¥ÖÐûÓÐÉèÖà enctype ÊôÐÔ
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚ±íµ¥Ìύʱ£¬ÈôÊÇ enctype ÊôÐÔûÓÐÉèÖã¬ÄÇôĬÈϵÄÊý¾Ý´«Êä·½·¨ÊÇ application/x-www-form-urlencoded¡£ÏÖÔÚ£¬Êý¾Ý½«±»°²ÅÅÔÚ HTTP ÇëÇóÍ·²¿¶ø·ÇÇëÇóÌåÖС£ÒÔÊÇ£¬ÔÚ»ñÈ¡ post Êý¾Ýʱ£¬ÎÒÃÇÐèҪʹÓà $this->request->post() »òÕß request()->post()¡£
½Ó¿ÚŲÓÃʱûÓÐÉèÖÃÇëÇóÍ·
ÔÚ½Ó¿ÚŲÓÃʱ£¬ÎÒÃÇÐèÒªÉèÖÃÏìÓ¦µÄÇëÇóÍ·£¬ºÃ±È Content-Type£ºapplication/json£¬²»È»Ð§ÀÍÆ÷ÎÞ·¨ÆÊÎöÊý¾Ý¡£ÈôÊÇûÓÐÉèÖà Content-Type£¬ÔòЧÀÍÆ÷ĬÒÔΪ application/x-www-form-urlencoded£¬¶ø´Ëʱ post µÄÊý¾Ý»á·ÅÔÚ http ÇëÇóÍ·ÖУ¬¶ø²»ÊÇÇëÇóÌåÖУ¬µ¼ÖÂÎÞ·¨×¼È·»ñÈ¡ post Êý¾Ý¡£
Èý¡¢½â¾öÒªÁì
ÉèÖÃ enctype ÊôÐÔ
ÔÚ±íµ¥ÖÐÌí¼Ó enctype=”multipart/form-data”£¬ÕâÑù¾ÍÄܹ»×¼È·»ñÈ¡ post Êý¾ÝÁË¡£
ÉèÖÃÇëÇóÍ·
ÔÚ½Ó¿ÚŲÓÃʱ£¬¿ÉÒÔʹÓà curl ÉèÖÃÇëÇóÍ·¡£Ê¾Àý´úÂëÈçÏ£º
$data = array( 'username' => 'admin', 'password' => '123456' ); $url = 'http://www.example.com/login'; $ch = curl_init(); $header = array( 'Content-Type: application/json', 'Content-Length: '.strlen(json_encode($data)) ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($ch); curl_close($ch);
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇthinkphp»ñÈ¡²»µ½postÊý¾ÝÔõÑù½â¾öµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡