Entry
How do I retrieve raw HTTP POST data into a PHP script?
How can I get POST data from a non-browser based application into PHP?
How should I do to get raw data whose content-type is application/x-www-form-urlencoded with PHP?
Aug 17th, 2005 13:07
Thomas Capote, Christian Schmidt, Jon Bahary, Ken Crismon, Nathan Wallace, Gary Chen,
While the solution shown below may work, PHP provides a much simpler
and efficient way to access the "raw POST contents". Just read it in
like this:
$data = file_get_contents('php://input'); // done!
The 'php://input' part is a built-in PHP stream-wrapper and will work
anywhere a URL would work; e.g.: fopen() or fpassthru().
You'll find related information in the "PHP input/output streams"
section of "Appendix L. List of Supported Protocols/Wrappers" of the
PHP manual.
==== PREVIOUS ANSWER ====
To do this you must set the content type in the request header to
something PHP doesn't recognize. For example, "Content-Type: xyz".
If
php3 doesn't recognize the content type it will assign the posted data
to: $HTTP_RAW_POST_DATA which is then readily available in the script.
See post.c in the source for details.
Here is some sample codde using the MFC CHttpConnection class:
char *szHeaders = _T("Content-Type: xyz\r\n");
int stat = pFile->SendRequest(szHeaders, strlen(szHeaders),
pszRequest, strlen(pszRequest));
If you are unable to control the content-type from the client, you can
instead post to a CGI script that changes the content-type and then
invokes PHP in command line version.
The following shell script does this:
#!/bin/sh
unset CONTENT_TYPE GATEWAY_INTERFACE SCRIPT_FILENAME
/usr/bin/php x.php
If you don't unset the variable SCRIPT_FILENAME, PHP appearently thinks
that is is being invoked in real CGI mode and thus doesn't use the
filename on the command line (x.php) as input file but instead tries to
interpret the shell script itself as a PHP script.
In order to use this approach you must be able to run CGI scripts on
the
webserver, and the command line version of PHP must be installed.