如何将PHP生成的Feed转换为XML

时间:2017-03-30 10:26:17

标签: php xml

我使用PHP SimpleXML生成了一个Feed in xml-feed.php文件,如何将输出正确“转移”到XML文件xml-feed.xml?

<option>

2 个答案:

答案 0 :(得分:1)

<?php
$xmlFeed = simplexml_load_file('https://feed_URL.com');
$xml = "<products>\n";
foreach ($xmlFeed->product as $prod) {
    $xml.= "<product>\n";
        foreach($prod as $param => $param_val) {
            if($param!="price"){
                $xml.= "<$param>$param_val</$param>\n";
            }else{
                $xml.= "<$param>".convertCurrency($param_val, "USD", "PLN")."</$param>\n";
            }
        }
    $xml.= "</product>\n";
}
$xml.= "</products>\n";
//Writing to file
file_put_contents("xml-feed.xml", $xml)

您只需要将它们保存在变量中并将它们写入文件后,而不是回显。

答案 1 :(得分:0)

谢谢!我做了另一种方法:) 至于xml-feed.xml,我已添加:

<?php
header('Content-Type: application/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>

和.htaccess:

AddHandler application/x-httpd-php56 .xml