1

I maintain a webshop that generates xml files of it's orders. These orders should be pushes to a URL of the fulfilment center using http-POST.

We build a script for this and tested it ones against the URL of the fulfilment center and it appears to work, but since we can't keep testing against that URL (as the FFC would think that it are real orders) I want to make my own test envoirement. Were I can dump the content sent with POST to one .xml per order again.

For this I tried several scripts including Dump XML Posts from 'php://input' to file, but none of them seem to work. Dumping to TXT would also be fine for testing purposes.

Does any of you have an idea what I can do?

Community
  • 1
  • 1

4 Answers4

2

you can use an extension for your browser for firefox, HTTP Resource Test

I've misunderstood the question

you can also do this, which imo is more simple it will append the content to a txt file

function debug($content){
$fh = fopen("postrequest.txt", 'a') or die("can't open file");
fwrite($fh, $content."\n");
fclose($fh);
}
$input = file_get_contents("php://input");
debug($input);
Pedro Bernardo
  • 973
  • 2
  • 8
  • 17
  • Thanks for your answer! However it doesn't really seem to help me. An automated script pushes the POST to a specified URL and I need to be able to see the output, therefor I need a script to dump what's in POST. – Raimond van Mouche Jan 17 '14 at 13:34
  • Ah, thanks. This works and I can definitely use it to test the script! I'll still follow with the other given solution as that would be even more perfect. Also, unfortunately I can't vote your post up as I have too less reputation... – Raimond van Mouche Jan 17 '14 at 17:23
  • One question though, the encoding is not fully correct. Do you know any solution for this (maybe just stating the encoding in the head?)? I'm getting lines like `payment%3E%0A++++++%3Cmethod%3Em2epropayment%3C%2Fmethod%3E%0A++++%3C%2Fpayment%3E%0A++%3C%2Forder%3E%0A%3C%2Fordercollection%3E%0A` – Raimond van Mouche Jan 17 '14 at 17:58
  • Scraping debug, replacing it with `$xml = urldecode($input);` and changing the order (first php://input, followed bij urldecode and ending with fopen-fwrite-fclose) worked out. This solution was brought to me by http://stackoverflow.com/users/1792799/vic-degraeve < many thanks for that! – Raimond van Mouche Jan 19 '14 at 18:36
1
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($_POST, array ($xml, 'addChild'));
print $xml->asXML();
Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
1

Try this simple array to xml conversion if $_POST having single dimension array.

$post_arr = $_POST;
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($post_arr , array ($xml, 'addChild'));
print $xml->asXML();

For multi-dimensional array: Refer this SO Answer:

https://stackoverflow.com/a/19051521/270037

Community
  • 1
  • 1
Kumar V
  • 8,810
  • 9
  • 39
  • 58
  • I used this code and added `$handle = fopen("yourxmlfile.xml", "w");fwrite($handle, $strxml);fclose($handle);` after `return $dom->saveXML();`this doesn't create a file. Any thoughts on the reason for this? (folder permissions are 777) – Raimond van Mouche Jan 17 '14 at 13:30
  • I can't edit anymore so I'm using this. Forgot to specify that I used the multi-dimensional array script you linked to, as the input is multi-dimensional. – Raimond van Mouche Jan 17 '14 at 13:43
  • No, I don't (checked log files etc.). I'm not sure about `$node_name="root"` though. The script that creates a .txt-file ( http://stackoverflow.com/posts/21186608/revisions ) works, but with the wrong encoding. So at least I'm sure the POST part works. – Raimond van Mouche Jan 17 '14 at 17:56
0

Solution with thanks to SO-UserVic Degraeve.

$input = file_get_contents("php://input");
$xml = urldecode($input);
$fh = fopen(time() . ".xml", 'a') or die ("Can't create file!");
fwrite($fh, $xml);
fclose($fh);
Community
  • 1
  • 1
  • that will also work, but just keep in mind that the 'a' in the fopen will append the content to the file with the same name. In the end, you'll have to scrap the file to understand where one request end and the other one starts – Pedro Bernardo Jan 21 '14 at 08:33