PHP How to read raw mail HTTP data

the remote server periodically requests my php page via HTTP HEAD (check only KeepAlive, this works). If the remote server registers the trigger, it sends me an XML format with data (in the original raw format). I can not find where the error or information is, how can I read the input.

I am trying to do this (no error messages are shown), but the result is empty.

ini_set('always_populate_raw_post_data', 'On');

$data1 = file_get_contents('php://input');
//var_dump($data1); //NULL
fwrite($fp, 'php://input: '.serialize($data1)."\n");

$data2 = $GLOBALS['HTTP_RAW_POST_DATA'];
//var_dump($data2); //NULL
fwrite($fp, 'GLOBALS HTTP_RAW_POST_DATA: '.serialize($data2)."\n");

$data3 = $HTTP_RAW_POST_DATA;
//var_dump($data3); //NULL
fwrite($fp, 'HTTP_RAW_POST_DATA: '.serialize($data3)."\n");

//print_r($_POST); //NULL
fwrite($fp, 'POST: '.serialize($_POST)."\n");


$dataPOST = trim(file_get_contents('php://input'));
$xmlData = simplexml_load_string($dataPOST);
fwrite($fp, 'BETA: '.$xmlData."\n");

Result in the log file:

HeadRequest at 2015-01-21 23:35:47
======================================================
php://input: s:0:"";
GLOBALS HTTP_RAW_POST_DATA: N;
HTTP_RAW_POST_DATA: N;
POST: a:0:{}
BETA: 

About the server: PHP version - 5.5.9, the server runs under Linux (Apache / 2.4.7 (Ubuntu)

Thanks and best regards, Petr

+4
source share
1 answer

got it and gave a more complicated solution.

Result (working code):

<?php
// validate read-only stream for read raw data from the request body
if(file_get_contents('php://input')=='')
{
    // THROW EXCEPTION
}
else
{   
    // get read-only stream for read raw data from the request body
    $strRequest = file_get_contents('php://input'); 

    // import request to xml structure
    $DOMDocumentRequest = new DOMDocument;
    $DOMDocumentRequest->loadXML($strRequest);        
}
?>

About the problem:

  • LAMP (Ubuntu 14.04 LTS),
  • LAMP (Ubuntu 14.04 LTS) WireShark Pcap, - Apache2
  • WAMP (MS Windows Server 2008R2 x64 XAMPP), alll

,  Petr

+4

All Articles