HTTP Remote Logging via POST

slicer123
slicer123 used Ask the Experts™
on
A third party software library is sending logging information via HTTP POST to a designated web server (Apache). However the format of the POST request is not of the form, "PARAM=VALUE". If the remote URL is a .php script, how can I get the raw POST data? I can change the CGI on the web server to be a perl script or anything if that makes things easier.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2008
Commented:
Most Valuable Expert 2011
Top Expert 2016
Commented:
Can you post a sample with the URLs - perhaps we can show you an example that will work for you.  Best, ~Ray
If it's sent via POST, you simply need to read from STDIN.  Grab the environment variable CONTENT_LENGTH and read that many characters from STDIN.
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Author

Commented:
How can I do this with PHP? I am currently trying to use Log4PHP, which is not really that well documented or widely used. I think the third party tool was dev'd using Log4J, a tomcat app. Here's my current remote logging php target and output (from Apache access.log). Thanks
$ cat index.php 
<?php
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
...
 */
define('LOG4PHP_DIR', '../../log4php/src/main/php');
define('LOG4PHP_CONFIGURATION', 'file.properties');
 
require_once LOG4PHP_DIR.'/LoggerManager.php';
$logger = LoggerManager::getRootLogger();
 
function getHeaders()
{
    $headers = array();
    foreach ($_SERVER as $k => $v)
    {
        if (substr($k, 0, 5) == "HTTP_")
        {
            $k = str_replace('_', ' ', substr($k, 5));
            $k = str_replace(' ', '-', ucwords(strtolower($k)));
            $headers[$k] = $v;
        }
    }
    return $headers;
}  
 
$logger->debug(getHeaders());
$logger->debug(apache_request_headers());
$logger->debug(isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : " hrpd none ");
$logger->debug($_POST['content']);
$logger->debug($_GET);
$logger->debug($_REQUEST);
if ($_POST) {
        foreach ($_POST as $key => $value) {
                $logger->debug('key '.$key.' value '.$value);
        }
        $logger->debug('got here');
} else if ($_GET) {
        $logger->debug('get');
}
$logger->debug('eoe');
?>
 
07/19/09 07:36:11,406 [2220] DEBUG root - array (
  'Cache-Control' => 'no-cache',
  'Pragma' => 'no-cache',
  'User-Agent' => 'Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_13',
  'Host' => 'localhost',
  'Accept' => 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
  'Connection' => 'keep-alive',
)
07/19/09 07:36:11,407 [2220] DEBUG root - array (
  'Cache-Control' => 'no-cache',
  'Pragma' => 'no-cache',
  'User-Agent' => 'Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_13',
  'Host' => 'localhost',
  'Accept' => 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2',
  'Connection' => 'keep-alive',
)
07/19/09 07:36:11,407 [2220] DEBUG root -  hrpd none 
07/19/09 07:36:11,407 [2220] DEBUG root - 
07/19/09 07:36:11,407 [2220] DEBUG root - 
07/19/09 07:36:11,408 [2220] DEBUG root - 
07/19/09 07:36:11,408 [2220] DEBUG root - eoe

Open in new window

<?php
$len = getenv("CONTENT_LENGTH");
$sin = fopen("php://stdin","r");
$contents = fread($sin,$len);
echo $contents;
?>

In that code, $contents will contain the entire contents of the POST.
Top Expert 2008
Commented:
Using php://input it can be done like this:
$post_data = file_get_contents('php://input');
$logger->debug($post_data);

Open in new window

Author

Commented:
Apache needed rewrite rule, then output to server logs correctly.

RewriteRule ^/RemoteLogger/log$ /RemoteLogger/log/index.php [L]

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial