简体   繁体   中英

PHP How read HTTP Raw Post Data

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 xml format with the data (in post raw format). I can not find where is mistake or information how can I read the input data.

I try this (no error show), but 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)

Thank you and best regards, Petr

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), does't work
  • If I run code on LAMP (Ubuntu 14.04 LTS) and install WireShark with Pcap, server crashed - I must reinstall Apache2
  • If I run code on WAMP (MS Windows Server 2008R2 x64 with XAMPP), alll is right

Best regards, Petr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM