简体   繁体   English

PHP如何读取HTTP Raw Post数据

[英]PHP How read HTTP Raw Post Data

remote server periodically queries to my php page via HTTP HEAD (check only KeepAlive, this works). 远程服务器定期通过HTTP HEAD查询到我的php页面(只检查KeepAlive,这有效)。 If the remote server registers a trigger, sends me xml format with the data (in post raw format). 如果远程服务器注册了一个触发器,请向我发送带有数据的xml格式(采用后期原始格式)。 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) 关于服务器:PHP版本是5.5.9,服务器在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 如果我在LAMP(Ubuntu 14.04 LTS)上运行代码,则不起作用
  • If I run code on LAMP (Ubuntu 14.04 LTS) and install WireShark with Pcap, server crashed - I must reinstall Apache2 如果我在LAMP(Ubuntu 14.04 LTS)上运行代码并使用Pcap安装WireShark,服务器崩溃 - 我必须重新安装Apache2
  • If I run code on WAMP (MS Windows Server 2008R2 x64 with XAMPP), alll is right 如果我在WAMP(带有XAMPP的MS Windows Server 2008R2 x64)上运行代码,那么alll是对的

Best regards, Petr 最好的问候,彼得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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