简体   繁体   English

如何在 PHP 中获取请求内容(正文)?

[英]How to get request content (body) in PHP?

I am sending a request to a php server with a XML in the content:我正在向内容中包含 XML 的 php 服务器发送请求:

POST /index3.php HTTP/1.0
Connection: Close
Accept: application/xml
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?>
<root />

I've checked in globals vars (like $_GET , $_POST , $_ENV , $_FILES , $_REQUEST ...) but they are all empty.我已经检查了全局变量(如$_GET$_POST$_ENV$_FILES$_REQUEST ...)但它们都是空的。

How could I retrieve the content in the server?如何检索服务器中的内容?

Try this试试这个

$xml = file_get_contents('php://input');

From the manual :手册

php://input is a read-only stream that allows you to read raw data from the request body. php://input是一个只读流,允许您从请求正文中读取原始数据。

Use file_get_contents("php://input") ( manual ).使用file_get_contents("php://input") (手册)。

In PHP older than 7.0 you could also use $HTTP_RAW_POST_DATA (depending on always_populate_raw_post_data setting).在 7.0 之前的 PHP 中,您还可以使用$HTTP_RAW_POST_DATA (取决于always_populate_raw_post_data设置)。

Try this:试试这个:

<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){
    $xml = $GLOBALS["HTTP_RAW_POST_DATA"];
    $file = fopen("data.xml","wb");
    fwrite($file, $xml);
    fclose($file);
    echo($GLOBALS["HTTP_RAW_POST_DATA"]);
}
?>

Hope this helps.希望这可以帮助。

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

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