Home >PHP Framework >ThinkPHP >What should I do if thinkphp5 post cannot get the value?
thinkphp5 post cannot get the value because TP5 uses the strpos function to find the app/json string in the content-type value of the Header. The solution is to set the content-type value of the Header to app/json. That’s it.
The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.
thinkphp5 What should I do if the post cannot get the value?
Solution to the problem that ThinkPHP5 cannot receive the Json parameters passed by the client Post
I have been learning API development during this period, and I have taken a course from the Internet and am following it. Just yesterday, I once again encountered the situation where the lecturer kept me debugging for half a day. The problem I encountered was that when I used Postman to test a certain Post interface of the API, no matter how I sent the request, it always prompted me that the parameter verification failed. During my debugging process, One time, I used the method of passing parameters through the Body table, and the API magically worked. So I judged that there was a problem with the Json transmission before, so I searched all over the Internet, but there was no solution [but Get two knowledge points and put them in End of article].
Regarding the problem of TP5, if Du Niang couldn’t do it, she could only check the source code, so I followed the error bit by bit and finally discovered the problem~
It turns out that TP5 uses the strpos function to find the app/json string in the content-type value of the Header, which means that you must set the content-type value of the Header to app/json, otherwise TP5 will not think you have passed it in. is a Json parameter and set the value to Request::post to be empty.
The above solves the problem that TP5 cannot receive the Json parameters passed by the client Post.
Two knowledge points:
Data php whose content-type is "application/json" cannot be directly recognized, so the $_POST array is empty. Solution: We only need to use $GLOBALS['HTTP_RAW_POST_DATA'] to take out the original data, and then json_decode it [Using this method, you need to set the always_populate_raw_post_data value in php.ini to On][$HTTP_RAW_POST_DATA, which contains the original data of POST data. But this is not a superglobal variable, to use it in a function it must be declared global , or use $GLOBALS['HTTP_RAW_POST_DATA'] instead. ];
You can use file_get_contents('php://input') instead of the above method to directly obtain the incoming Json parameters [When I read the TP5 source code, I found that they also use This method to obtain the initial value].
Recommended learning: "thinkPHP Video Tutorial"
The above is the detailed content of What should I do if thinkphp5 post cannot get the value?. For more information, please follow other related articles on the PHP Chinese website!