1. Problem
After submitting the form, use request->param() or $this->request->param() The post data cannot be obtained and an empty array is obtained.
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, the default data transmission method is application/x-www-form-urlencoded. Data will now be placed in HTTP request headers 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 the interface is called
When the interface is called, we need to set the corresponding request header, such as Content-Type: application /json, otherwise the server cannot parse the 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=" in the form multipart/form-data", so that the post data can be obtained correctly.
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);
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

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

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

Undress AI Tool
Undress images for free

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
