Tag Archives: PHP

PHP Wrappers, Reading XML with php://input

Most of us are used to utilizing the request object in PHP to access the associative array created by form posts. Most traditionally, $_GET, $_POST or if you are really lazy like me, $_REQUEST.

Newer versions of PHP (5.1+) also allow direct access to input streams. It’s a little less memory intensive than traditional ways, though you should be aware that it cannot be used when forms are submitted with enctype=”multipart/form-data” because of their reliance on writing submitted resources to disk.

More impressively, it allows access to ANY data input to PHP

I recently wrote a server API that had to accept XML objects and parse them for data.

$xml_object	= file_get_contents('php://input');	# read submitted xml
$xml		= simplexml_load_string($xml_object);	# extract contents into useable form
$id 		= $xml->id;

Once the php input data is stored as an object, you can parse it with the built in PHP function simplexml_load_string which immediately makes individual node values accessible by their name.