23

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?

YakovL
  • 7,557
  • 12
  • 62
  • 102
Salazar
  • 401
  • 1
  • 3
  • 8
  • Related: https://stackoverflow.com/q/8945879/3995261 – YakovL Feb 14 '18 at 13:57
  • 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." https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html – Anthony Rutledge Nov 22 '19 at 15:43
  • Instead of sending the HTTP `Accept` header, I might suggest sending the HTTP `Content-Length` header. – Anthony Rutledge Nov 22 '19 at 15:45

3 Answers3

34

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
  • This needs more hype as it is very important part of a php developer and right now its like a hidden gem – Dimitar Mar 26 '19 at 11:19
  • 1
    If `php://input` is a hidden gem, then stream filters for handling encoding would be the entire ring. https://www.php.net/manual/en/function.stream-filter-append.php – Anthony Rutledge Nov 22 '19 at 15:49
4

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).

piotrp
  • 3,755
  • 1
  • 24
  • 26
1

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.

talha2k
  • 24,937
  • 4
  • 62
  • 81