Dixos
Dixos

Reputation: 93

PHP won't receieve POST XML data

I'm building a service backend that is being sent a "delivery report" after successfully sending a SMS to a user.

The report itself is XML POSTed to our "endpoint" with the content-type application/xml.

I'm using Postman to make sure that everything is working correctly. I've made a test using regular JSON and can return the data without issues, however, no matter what I try with XML I basically get no indication that anything is being sent to the server.

(Test with JSON) Server response test with JSON

(Test with XML) Server response test with XML

Here's my simple PHP script:

<?php
header('Content-Type: application/xml; charset=utf-8');

print_r(json_decode(file_get_contents("php://input"), true));
print_r($HTTP_RAW_POST_DATA);
?>

I feel like I've tried everything. Been looking at past issues posted to SO and other places, it simply won't work for me. I'm hoping for some answers that at least points me in the right direction here.

Cheers.

Upvotes: 0

Views: 1065

Answers (2)

hanshenrik
hanshenrik

Reputation: 21473

json_decode can't read XML, seems like you're trying to parse XML with json_decode. if you want to output the received XML, use echo (or if it's for debugging purposes, use var_dump, eg var_dump(file_get_contents("php://input"));), or if you want to parse the XML, use DOMDocument.

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

Your trying to json_decode XML data. You should use something like SimpleXML. Instead of...

print_r(json_decode(file_get_contents("php://input"), true));

You should use ...

$xml = new SimpleXMLElement(file_get_contents("php://input"));
echo $xml->asXML();

You should be able to get the information by (for example)...

echo (string)$xml->id;

Upvotes: 1

Related Questions