PHP:: Get POST Request Content

半城伤御伤魂 提交于 2019-12-24 16:20:03

问题


I'm sending a POST request using WebClient.UploadData() method (C#) to my webserver. The packet sent to my webserver looks like so:

POST / HTTP/1.1
Host: {ip}
Content-Length: {length}
Expect: 100-continue
Connection: Keep-Alive


{buffer_content}

As the {buffer_content} is nowhere assigned in the $_POST array, I have the following question...

Question: How do I read the {buffer_content} with PHP?

I've stumbled upon file_get_contents('php://input'), but I'm unsure whether that is recommended to do.


回答1:


Use the php://input stream:

$requestBody = file_get_contents('php://input');

This is the recommended way to do this and, in PHP 7.0, the only way. Previously, there was sometimes a global variable called $HTTP_RAW_POST_DATA, but whether it existed would depend on an INI setting, and creating it hurt performance. That variable was deprecated and removed.

Beware that prior to PHP 5.6, you can only read php://input once, so make sure you store it.

Once you have your body, you can then decode it from JSON or whatever, if you need that:

$requestBody = json_decode($requestBody) or die("Could not decode JSON");


来源:https://stackoverflow.com/questions/34371656/php-get-post-request-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!