4

The remote server periodically queries to my PHP page via HTTP HEAD (check only KeepAlive - this works). If the remote server registers a trigger, sends me the XML format with the data (in POST raw format). I can not find where the mistake is or information on how I can read the input data.

I try this (no error show), 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 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 server: PHP version is 5.5.9, Server run on Linux (Apache/2.4.7 (Ubuntu)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
czWolfHunter
  • 387
  • 2
  • 5
  • 17

1 Answers1

4

I got it and give a more complex 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 a problem:

  • If I run code on LAMP (Ubuntu 14.04 LTS (Trusty Tahr)), it doesn’t work
  • If I run code on LAMP (Ubuntu 14.04 LTS) and install Wireshark with Pcap, the server crashed - I must reinstall Apache 2
  • If I run code on WAMP (Windows Server 2008 R2 x64 with XAMPP), all is right
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
czWolfHunter
  • 387
  • 2
  • 5
  • 17
  • Do not just automatically use data from php://input without some form of filtering and validation. Ideally, you would also perform a UTF-8 encoding check first by using a stream filter. Also, you should be url decoding the data you from php://input before using it. Otherwise, you may not get the data the way it was intended to be represented. With $_POST, the url decoding has already taken place! – Anthony Rutledge Jul 19 '21 at 10:20