18
votes

I am sending a request to a php server with a XML in the content:

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.

How could I retrieve the content in the server?

3
The HTTP Accept header "... can be used to specify certain media types which are acceptable for the response." Are you saying here that you desire PHP to respond with XML? "... If no Accept header field is present, then it is assumed that the client accepts all media types. If an Accept header field is present, and if the server cannot send a response which is acceptable according to the combined Accept field value, then the server SHOULD send a 406 (not acceptable) response." w3.org/Protocols/rfc2616/rfc2616-sec14.htmlAnthony Rutledge
Instead of sending the HTTP Accept header, I might suggest sending the HTTP Content-Length header.Anthony Rutledge

3 Answers

28
votes

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.

3
votes

Use file_get_contents("php://input") (manual).

In PHP older than 7.0 you could also use $HTTP_RAW_POST_DATA (depending on always_populate_raw_post_data setting).

1
votes

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.