0

I need to store XML data sent over HTTP POST to my server. In the log files I see that the data is successfully sent to my server. But I have no idea how to get the data.

I tried to catch them with the php://input stream like in the code below. The problem I see is that php://input is just read when the file containing the code is called.

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

$var_str = var_export($xml, true);
file_put_contents('api-test/test.txt', $var_str);

Is there any way to set some kind of listener/watcher to the php://input stream? Maybe PHP is the wrong technology to realize this. Is there some other way like AJAX?

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • How is https://stackoverflow.com/questions/8207488/get-all-variables-sent-with-post remotely a duplicate? – Quentin Aug 28 '19 at 12:49
  • @Machavity — $_POST is wildly irrelevant as XML is being POSTed not URL Enccoded or Multipart Form data. – Quentin Aug 28 '19 at 12:50

1 Answers1

0

The problem I see is that php://input is just read when the file containing the code is called.

Yes.

That's how PHP (in a server-side programming context) works.

  1. The client makes an HTTP request to a URL
  2. The server receives the HTTP request and determines that that URL is handled by a particular PHP program (typically by matching the path component of the URL to a directory and file name unless the Front Controller Pattern is being used)
  3. The PHP program is executed and has access to data from the request
  4. The server sends the output of the PHP program back

Is there any way to set some kind of listener/watcher to the php://input stream?

You get a new stream every time a request is made. So the typical way to watch it is to put a PHP script at the URL that the request is being made to.

Then make sure each request is made to the same URL.

(If you need to support requests being made to different URLs, then look into the Front Controller Pattern).

Maybe PHP is the wrong technology to realize this.

It's a perfectly acceptable technology for handling HTTP requests.

Is there some other way like AJAX?

Ajax is a buzzword meaning "Make an HTTP request with JavaScript". Since you are receiving the requests and not making them, Ajax isn't helpful.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335