Recipe 8.7. Reading the Post Request Body


8.7.1. Problem

You want direct access to the body of a post request, not just the parsed data that PHP puts in $_POST for you. For example, you want to handle an XML document that's been posted as part of a web services request.

8.7.2. Solution

Read from the php://input stream, as in Example 8-15.

Reading the post request body

<?php $body = file_get_contents('php://input'); ?>

8.7.3. Discussion

The auto-global array $_POST is great when you just need access to submitted form variables, but it doesn't cut it when you need raw, uncut access to the whole request body. That's where the php://input stream comes in. Read the entire thing with file_get_contents( ), or if you're expecting a large request body, read it in chunks with fread( ).

If the configuration directive always_populate_raw_post_data is on, then raw post data is also put into the global variable $HTTP_RAW_POST_DATA. But to write maximally portable code, you should use php://input instead'that works even when always_populate_raw_post_data is turned off.

8.7.4. See Also

Documentation on php://input at http://www.php.net/wrappers and on always_populate_raw_post_data at http://www.php.net/ini.core#ini.always-populate-raw-post-data.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net