简体   繁体   English

使用php将POST数据转储到XML

[英]Dump POST data to XML using php

I maintain a webshop that generates xml files of it's orders. 我维护了一个网上商店,该商店会生成订单的xml文件。 These orders should be pushes to a URL of the fulfilment center using http-POST. 这些订单应使用http-POST推送到履行中心的URL。

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. 我们为此构建了一个脚本,并针对履行中心的URL对其进行了测试,它似乎可以正常工作,但是由于我们无法继续对该URL进行测试(因为FFC认为这是真实的订单),因此我想我自己的测试环境。 Were I can dump the content sent with POST to one .xml per order again. 我是否可以将POST发送的内容再次转储到每个订单一个.xml。

For this I tried several scripts including Dump XML Posts from 'php://input' to file , but none of them seem to work. 为此,我尝试了几种脚本,包括将XML Posts从'php:// input'转储到file ,但是似乎没有一个起作用。 Dumping to TXT would also be fine for testing purposes. 出于测试目的,转储到TXT也可以。

Does any of you have an idea what I can do? 你们中有人知道我能做什么吗?

you can use an extension for your browser for firefox, HTTP Resource Test 您可以将浏览器的扩展名用于Firefox, HTTP资源测试

I've misunderstood the question 我误解了这个问题

you can also do this, which imo is more simple it will append the content to a txt file 您也可以这样做,哪一种imo更简单,它将内容附加到txt文件

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);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($_POST, array ($xml, 'addChild'));
print $xml->asXML();

Try this simple array to xml conversion if $_POST having single dimension array. 如果$ _POST具有一维数组,请尝试将此简单数组转换为xml。

$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: 对于多维数组:请参阅此SO答案:

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

Solution with thanks to SO-User Vic Degraeve . 感谢SO-User Vic 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM