亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home PHP Framework ThinkPHP How to solve the problem that thinkphp cannot obtain post data

How to solve the problem that thinkphp cannot obtain post data

Apr 08, 2023 am 09:30 AM

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

  1. 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().

  1. 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

  1. Set the enctype attribute

Add enctype="multipart/form-data" to the form so that it can be obtained correctly post data.

  1. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72