Return to Snippet

Revision: 797
at August 9, 2006 05:05 by mthorn


Updated Code
/**
     Using php://input
     -----------------
     Version: 4.3+
     
     Reading [php://input][1] is the preferred method since PHP 4.3.
     
     The Content-Type header of the posted content must _NOT_ be 
     application/x-www.form-urlencoded or multipart/form-data.
     
     For XML you can use the Content-Type: text/xml.
     
      [1]: http://us2.php.net/manual/en/wrappers.php.php
    */

    // Make sure the user is posting
    if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
    {
        // Read the input from stdin
        $postText = trim(file_get_contents('php://input'));
    }
  
    /**
     Using $HTTP_RAW_POST_DATA
     -------------------------
     Version 3.0+
     
     __Note__: Reading php://input is preferred.
     
     
     As with php://input, the Content-Type header of the 
     posted content must _NOT_ be application/x-www.form-urlencoded or
     multipart/form-data.     
     
     PHP 4.1+
     ~~~~~~~~
     You can also enable [always-populate-raw-post-data][1] in the php.ini to
     have the value always populated reguardless of Content-Type. However
     since this requires config changes it is less portable.
     
      [1]: http://us2.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
    */
    $postText = $GLOBALS['HTTP_RAW_POST_DATA'];

Revision: 796
at August 9, 2006 05:03 by mthorn


Initial Code
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.


<?php
    /**
     Using php://input
     -----------------
     Version: 4.3+
     
     Reading [php://input][1] is the preferred method since PHP 4.3.
     
     The Content-Type header of the posted content must _NOT_ be 
     application/x-www.form-urlencoded or multipart/form-data.
     
     For XML you can use the Content-Type: text/xml.
     
      [1]: http://us2.php.net/manual/en/wrappers.php.php
    */

    // Make sure the user is posting
    if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
    {
        // Read the input from stdin
        $postText = trim(file_get_contents('php://input'));
    }
  
    /**
     Using $HTTP_RAW_POST_DATA
     -------------------------
     Version 3.0+
     
     __Note__: Reading php://input is preferred.
     
     
     As with php://input, the Content-Type header of the 
     posted content must _NOT_ be application/x-www.form-urlencoded or
     multipart/form-data.     
     
     PHP 4.1+
     ~~~~~~~~
     You can also enable [always-populate-raw-post-data][1] in the php.ini to
     have the value always populated reguardless of Content-Type. However
     since this requires config changes it is less portable.
     
      [1]: http://us2.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
    */
    $postText = $GLOBALS['HTTP_RAW_POST_DATA'];
?>

Initial URL
http://www.mthorn.net

Initial Description
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.

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

Initial Tags
ajax, php, actionscript, post, flash, xml

Initial Language
PHP