Chose Language:
comments
Author: Admin/Publisher |finished | checked

php//input

The stream php://input is readonly, this one permits read raw data from the body requested.

When we obtain the data with the superglobal variable $_POST, this one is for formulary data and update files (classic HTML Formulary)

  • application/x-www-form-urlencoded ( form-posts)
  • multipart/form-data (file uploads)
Important:
  • we can not use php//input with enctype="multipart/form-data"
  • php://input is a better option than $HTTP_RAW_POST_DATA, cause it not depends on php.ini directives

AJAX and php//input

Many times php://input is preferable using it with AJAX, of corse is not that $_POST not work but php//input has no process its raw_data.

Also, keep in mind that we would most likely want to receive JSON content (content will be application/json)

¿Ok, how we use it?

If you go to the oficial PHP page probably depending on language you will not find any example of php://input, it assumes that you know to use all php://(I/O streams) with this lack of examples.

We can use a file_get_contents to obtain this data from this stream.

php code to obtain input data
$inputdata = file_get_contents("php://input");

Let’s make an example with AJAX and get data with it. I will take the next JQUERY example.

we will take this example and change it
$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
});

We will modify it and make a file like next one:

ajaxpage.html
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>example of php://input</title>
</head>
<body>
<main>
	<div id="resultado">
		<p>el resultado va aqui:</p>
	</div>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"> </script>
	<script>
		//Blastcoding.com
	$.ajax({
	method: "POST", //send method
	url: "inputs.php", //URL 
	data: { name: "John", location: "Boston" } //data to be send
	})
	.done(function( msg ) {
		$( "#resultado" ).append( msg );
	});
	</script>
</body>
</html>

We can do the following thing for our PHP file:

inputs.php
<?php
$inputsdata = file_get_contents("php://input");
echo $inputsdata;

Between these 2 files we get the following result:

el resultado va aqui:
name=John&location=Boston

From here we could use the function parse_str and do something similar to what we do with $_POST.

Let see something different now

What happen if we change the default value of proccessData to false in our $.ajax

cambio en código de ajaxpage.html
<script>
	//Blastcoding.com
	$.ajax({
	method: "POST", //metodo con el que sera enviada
	url: "inputs.php", //URL donde sera envida
	data: { name: "John", location: "Boston" }, //data que sera enviada
        processData:false
	})
	.done(function( msg ) {
		$( "#resultado" ).append( msg );
	});
</script>

the result would be:

el resultado va aqui:
[object Object]

And this is the result that we spected, since the obtained in our PHP is an object, but to can use it we need to pass it as string, so lets stringify it.

ajaxpage.html(modificado)
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>example of php://input</title>
</head>
<body>
<main>
	<div id="resultado">
		<p>el resultado va aqui:</p>
	</div>
</main>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"> </script>
	<script>
		//Blastcoding.com
	$.ajax({
	method: "POST", //metodo con el que sera enviada
	url: "inputs2.php", //URL donde sera envida
	data:  JSON.stringify({name: "John",location: "Boston"}), //data que sera enviada
	processData:false
	})
	.done(function( msg ) {
		$( "#resultado" ).append(msg);
	});
	</script>
</body>
</html>

On the other hand, our PHP would be the following, in my case I decided to do it in a new PHP file.

inputs2.php
<?php
$inputsdata = file_get_contents("php://input");
 $arr = json_decode($inputsdata,true);
 $message = <<<HTML
 name : {$arr['name']}, location: {$arr['location']}
 HTML;
 
 echo $message;
In versions <PHP 7.3, the exposed heredoc could give us a PHP Parse error: syntax error, unexpected end. This error is due to HTML; has space before or after or on both sides
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments