When I was using ThinkPHP to develop a project recently, I encountered a problem: after submitting the form, the post data could not be obtained. This is a common problem during the development process. Sometimes we will feel very confused, especially when we have found many methods on the Internet and still cannot solve the problem. This article will briefly introduce how to solve this problem.
1. Problem phenomenon
After submitting the form, the post data cannot be obtained through request->param() or $this->request->param(). What is obtained is Empty array.
2. Cause of the problem
- The enctype attribute is not set in the form
When the form is submitted, if the enctype attribute is not set, then the default data transmission The method is application/x-www-form-urlencoded. At this time, the post data will be placed in the http request header instead of the request body. Therefore, when getting post data, we need to use $this->request->post() or request()->post().
- No request header is set when calling the interface
When calling the interface, we need to set the corresponding request header, such as Content-Type: application/json, otherwise the server cannot Analytical data. If Content-Type is not set, the server defaults to application/x-www-form-urlencoded, and at this time the post data will be placed in the http request header instead of the request body, resulting in the inability to obtain the post data correctly.
3. Solution
- Set the enctype attribute
Add enctype="multipart/form-data" to the form so that it can be obtained correctly post data.
- Set request header
When calling the interface, you can use curl to set the request header. The sample code is as follows:
$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);
4. Summary
Failure to obtain post data is a common problem. This situation is generally caused by incorrect data transmission methods or incorrect request header settings. If you encounter this problem, you can solve it one by one according to the above methods. Of course, you can also use other methods, such as using php://input or $_POST to obtain post data. Finally, I hope this article can solve similar problems that readers encounter during the development process.
The above is the detailed content of How to solve the problem that thinkphp cannot obtain post data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)