Php://input example

Hello,
could anyone show me how to use the php://input wrapper? I checked the manual but couldn’t find the examples helpful.
I would like to get the rough content of an HTTP POST.

thanks in advance for your time :slight_smile:

You can treat "php://input " as plain filename and use it everywhere filename makes sense (fopen(), file(), file_get_contents() etc).

Complete example: simple XML-based client-server application. Client part sends xml requests to the server via XMLHTTPRequest. Server part reads xml from php://input and replies it back to the client.



<?php
// SERVER

if(getenv('REQUEST_METHOD') == 'POST') {
	$client_data = file_get_contents("php://input");
	echo "
		<SERVER>
			Hallo, I am server
			This is what I've got from you
			$client_data
		</SERVER>
	";
	exit();
}
?>

<!-- CLIENT -->

<script>
function sendAndLoad(sURL, sXML) {
	var r = null;
	if(!r) try { r = new ActiveXObject("Msxml2.XMLHTTP") } catch (e){}
	if(!r) try { r = new XMLHttpRequest() } catch (e){}
	if(!r) return null;
	r.open("POST", sURL, false);
	r.send(sXML);
	return r.responseText;
}
</script>

<button onclick="
	alert(sendAndLoad(
			location.href,
			'<xml><sample>data</sample></xml>'
	))">
Test
</button>

thank you so much :slight_smile:
will try that.

hey I have one more question :o
Since you seem used to this process of making two apps communicating with each other, I was wondering, how would you typically handle incoming xml documents?

I mean, if an application send to my server an xml document in the body of a HTTP POST, what is the best way to handle it?

Here is a typical example:


<Message>
  <text>hello</text>
  <user>benno</user>
  <lang>en</lang>
</Message>

I’d use DOM_XML (php4) or DOM (php5) functions

http://www.php.net/ref.domxml
http://www.php.net/ref.dom

wow… that’s some new horizons :slight_smile:

I’ll check it out all. Any hint (trick) to get the value of a given tag maybe?

you’re such a great help man!

To get any given tag (or node) in a DOM tree, you can use DOM navigation functions (for example, http://www.php.net/function.dom-domdocument-getelementbyid) or XPath (http://www.php.net/function.dom-domxpath-query).

great!
that was some serious and quick help.
thank you very much for your patience and your assistance.
Regards.