Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get body of a POST in php?

Tags:

rest

post

php

I submit as POST to a php page the following:

{a:1} 

This is the body of the request (a POST request).
In php, what do I have to do to extract that value?

var_dump($_POST);  

is not the solution, not working.

like image 744
Itay Moav -Malimovka Avatar asked Jan 20 '12 18:01

Itay Moav -Malimovka


People also ask

What is $_ POST in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.

What is raw POST data?

If the data is in another format, such as JSON, or if the data doesn't conform to PHP's quirks (such as the rules for having [] on the end of keys with the same name) then you might want to access the data directly so you can parse it yourself. That is the raw POST data.

What is get and post method in PHP?

Get and Post methods are the HTTP request methods used inside the <form> tag to send form data to the server. HTTP protocol enables the communication between the client and the server where a browser can be the client, and an application running on a computer system that hosts your website can be the server.

How post URL in PHP?

If you're looking to post data to a URL from PHP code itself (without using an html form) it can be done with curl. It will look like this: $url = 'http://www.someurl.com'; $myvars = 'myvar1=' . $myvar1 .


1 Answers

To access the entity body of a POST or PUT request (or any other HTTP method):

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

Also, the STDIN constant is an already-open stream to php://input, so you can alternatively do:

$entityBody = stream_get_contents(STDIN); 

From the PHP manual entry on I/O streamsdocs:

php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of $HTTP_RAW_POST_DATA as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data. php://input is not available with enctype="multipart/form-data".

Specifically you'll want to note that the php://input stream, regardless of how you access it in a web SAPI, is not seekable. This means that it can only be read once. If you're working in an environment where large HTTP entity bodies are routinely uploaded you may wish to maintain the input in its stream form (rather than buffering it like the first example above).

To maintain the stream resource something like this can be helpful:

<?php  function detectRequestBody() {     $rawInput = fopen('php://input', 'r');     $tempStream = fopen('php://temp', 'r+');     stream_copy_to_stream($rawInput, $tempStream);     rewind($tempStream);      return $tempStream; } 

php://temp allows you to manage memory consumption because it will transparently switch to filesystem storage after a certain amount of data is stored (2M by default). This size can be manipulated in the php.ini file or by appending /maxmemory:NN, where NN is the maximum amount of data to keep in memory before using a temporary file, in bytes.

Of course, unless you have a really good reason for seeking on the input stream, you shouldn't need this functionality in a web application. Reading the HTTP request entity body once is usually enough -- don't keep clients waiting all day while your app figures out what to do.

Note that php://input is not available for requests specifying a Content-Type: multipart/form-data header (enctype="multipart/form-data" in HTML forms). This results from PHP already having parsed the form data into the $_POST superglobal.

like image 197
rdlowrey Avatar answered Sep 30 '22 05:09

rdlowrey