Read raw post data, useful for grabbing XML from Flash XmlSocket.


/ Published in: PHP
Save to your folder(s)

Often PHP can't interpret POST data because it is not form-encoded. This is typical when the post data is XML from API's like Flash's XmlSocket. You can use the following methods to read the POST data directly.


Copy this code and paste it in your HTML
  1. /**
  2.   Using php://input
  3.   -----------------
  4.   Version: 4.3+
  5.  
  6.   Reading [php://input][1] is the preferred method since PHP 4.3.
  7.  
  8.   The Content-Type header of the posted content must _NOT_ be
  9.   application/x-www.form-urlencoded or multipart/form-data.
  10.  
  11.   For XML you can use the Content-Type: text/xml.
  12.  
  13.   [1]: http://us2.php.net/manual/en/wrappers.php.php
  14.   */
  15.  
  16. // Make sure the user is posting
  17. if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
  18. {
  19. // Read the input from stdin
  20. $postText = trim(file_get_contents('php://input'));
  21. }
  22.  
  23. /**
  24.   Using $HTTP_RAW_POST_DATA
  25.   -------------------------
  26.   Version 3.0+
  27.  
  28.   __Note__: Reading php://input is preferred.
  29.  
  30.  
  31.   As with php://input, the Content-Type header of the
  32.   posted content must _NOT_ be application/x-www.form-urlencoded or
  33.   multipart/form-data.
  34.  
  35.   PHP 4.1+
  36.   ~~~~~~~~
  37.   You can also enable [always-populate-raw-post-data][1] in the php.ini to
  38.   have the value always populated reguardless of Content-Type. However
  39.   since this requires config changes it is less portable.
  40.  
  41.   [1]: http://us2.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
  42.   */
  43. $postText = $GLOBALS['HTTP_RAW_POST_DATA'];

URL: http://www.mthorn.net

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.