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)

5
  • A HEAD request has no POST data … Commented Jan 21, 2015 at 23:50
  • Did you use fopen first for $fp? Commented Jan 21, 2015 at 23:53
  • Yes, I use fopen etc. - here I add only all methods, which I try for read values from HTTP RAW POST. Commented Jan 21, 2015 at 23:57
  • HEAD request is only KeepAlive. If the remote server registers a trigger, sends me xml format with the data (in post raw format). Commented Jan 21, 2015 at 23:58
  • Can you try by suppressing the error by adding @file_get_contents('php://input'); Commented Jan 22, 2015 at 4:51

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.