如何将内容类型的text / xml添加到PHP代码创建的XML文件中?

时间:2019-04-26 08:42:55

标签: php xml

我有一个PHP代码,可将产品与数据库一起导出到XML文件。 创建的XML文件存在问题,因为其内容类型设置为“ text / html”,我需要将其更改为“ text / xml”

此代码对我有用,但在文本/ html中创建xml。

<?php 
    $con = mysqli_connect("xxx","xxx","xxx","xxx");
    mysqli_set_charset($con,"utf8");
    $sql = "SELECT * FROM products WHERE ";
    $myFile = "xml/xxx.xml";

    $fh = fopen($myFile, 'W +');
    $rss_txt = "";
    $rss_txt .= '<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL;
        $rss_txt .= '<products>'.PHP_EOL;
        $rss_txt .= '<datetime><![CDATA['.date('d.m.Y H:i').']]></datetime>'.PHP_EOL;
            $query = mysqli_query($con, $sql);

            if (!$query) {
                die(mysql_error());
            }

            while($values_query = mysqli_fetch_assoc($query)){
                    $rss_txt .= '<product>'.PHP_EOL;
                        $rss_txt .= '<title><![CDATA[' .$values_query['product']. ']]></title>'.PHP_EOL;
                        $rss_txt .= '<sku><![CDATA[' .$values_query['sku']. ']]></sku>'.PHP_EOL;
                        $rss_txt .= '<description><![CDATA[' .$values_query['description']. ']]></description>'.PHP_EOL;
                        $rss_txt .= '<image><![CDATA[' .$values_query['img']. ']]></image>'.PHP_EOL;
                        $rss_txt .= '<quantity><![CDATA[' .$values_query['stock']. ']]></quantity>'.PHP_EOL;
                        $rss_txt .= '<category><![CDATA[' .$values_query['product_categories']. ']]></category>'.PHP_EOL;
                        $rss_txt .= '<price><![CDATA[' .$values_query['price']. ']]></price>'.PHP_EOL;
                    $rss_txt .= '</product>'.PHP_EOL;
            }

        $rss_txt .= '</products>'.PHP_EOL;

    fwrite($fh, $rss_txt);
    fclose($fh);

    mysqli_close($con);
?>

我知道我将通过添加以下代码来更改内容类型:

header("Content-Type: text/xml");

0 个答案:

没有答案