首页 > 解决方案 > 如何将标头添加到用 PHP 创建的 XML 文件

问题描述

我有一个代码,它从数据库中获取数据并创建一个 XML 文件。我需要将此类标头添加到 xml 文件中。需要的标题:

header("Content-type: text/xml");
header('Content-Desposition: attachment; filename="products.xml"');

原始的php代码:

<?php

      include('dbconnect.php');

      function loadXML(){

           global $dbh;
           $result = $dbh->query("SELECT * FROM stock_infos;");
           $name = strftime('products.xml');
           $xml = new SimpleXMLElement('<nuts/>');
           while($row = $result->fetch(PDO::FETCH_ASSOC)){

                $nut = $xml->addChild('nut');
                $nut->addChild('code',$row['code_product']);
                $nut->addChild('name_product',$row['name_product']);
                $nut->addChild('in_stock',$row['in_stock']);
                $nut->addChild('date_stock',$row['date_stock']);


           }
           $dom = new DOMDocument('1.0');
           $dom->preserveWhiteSpace = false;
           $dom->formatOutput = true;
           $dom->loadXML($xml->asXML());
           $dom->save(dirname(__FILE__).'/'.$name);



      }
      loadXML();
?>

代码最终应该是什么样子,以便生成带有所需标头的 XML 文件?

谢谢!

标签: phpxml

解决方案


推荐阅读