PHP DOMDocument输出HTML页面代码


PHP DOMDocument output HTML page code

我制作了一个web应用程序,用户在其中输入数据,然后用ms-sql服务器数据库中的值生成xml,因为用户将在服务器上本地使用它,所以我需要触发下载,而不是保存到一个位置,在这样做的同时,输出还包括页面中的HTML,这是代码

if(isset($_POST['Enviar']))
{
    $dia = $_POST['dia'];
    $mes = $_POST['mes'];
    $anio = $_POST['anio'];
    $fechao = $dia.'/'.$mes.'/'.$anio;
    $rifr = 'J'.$_POST['rifr'];
    $cod = $_POST['cod'];
    $xml = new DOMDocument('1.0', 'ISO-8859-1');
    $sql = "SELECT        REPLACE(REPLACE(REPLACE(dbo.SAPROV.ID3, '-', ''), '.', ''), ' ', '') AS RIF, RIGHT(REPLACE(REPLACE(REPLACE(SAACXP_1.NumeroD, '-', ''), 'a', ''), ' ', ''), 8) 
                             AS N_FACTURA, RIGHT(REPLACE(REPLACE(SAACXP_1.NroCtrol, '-', ''), ' ', ''), 8) AS NroCtrol, '0' + dbo.SAPAGCXP.CodRete AS CodOper, 
                             dbo.SAPAGCXP.BaseReten AS Monto, ISNULL(dbo.SAOPER.Clase, '2') AS Porcentaje, SUBSTRING(CONVERT(varchar, dbo.SAACXP.FechaE, 112), 1, 6) AS Mes
    FROM            dbo.SAOPER RIGHT OUTER JOIN
                             dbo.SAACXP AS SAACXP_1 RIGHT OUTER JOIN
                             dbo.SAPAGCXP RIGHT OUTER JOIN
                             dbo.SAPROV INNER JOIN
                             dbo.SAACXP ON dbo.SAPROV.CodProv = dbo.SAACXP.CodProv ON dbo.SAPAGCXP.NumeroD = dbo.SAACXP.NumeroD ON 
                             SAACXP_1.NroUnico = dbo.SAACXP.NroRegi ON dbo.SAOPER.CodOper = dbo.SAACXP.CodOper
    WHERE        (dbo.SAACXP.TipoCxP = '21')";
    $stmt = sqlsrv_query($conex, $sql) or die('Query failed!');
    while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
    {
        $root = $xml->createElement("RelacionRetencionesISLR");
        $xml->appendChild($root);
        $root->setAttribute("RifAgente", $row['RIF']);
        $root->appendChild($xml->createAttribute('Periodo'))->appendChild($xml->createTextNode($row['Mes'])); 
        //
        $a1 = $xml->createElement("RifRetenido");
        $rif = $xml->createTextNode($rifr);
        $a1->appendChild($rif);
        //
        $a2 = $xml->createElement("NumeroFactura");
        $nfactura = $xml->createTextNode($row['N_FACTURA']);
        $a2->appendChild($nfactura);
        //
        $a3 = $xml->createElement("NumeroControl");
        $ncontrol = $xml->createTextNode($row['NroCtrol']);
        $a3->appendChild($ncontrol);
        //
        $a4 = $xml->createElement("FechaOperacion");
        $fecha = $xml->createTextNode($fechao);
        $a4->appendChild($fecha);
        //
        $a5 = $xml->createElement("CodigoConcepto");
        $concepto = $xml->createTextNode($cod);
        $a5->appendChild($concepto);
        //
        $a6 = $xml->createElement("MontoOperacion");
        $monto = $xml->createTextNode($row['Monto']);
        $a6->appendChild($monto);
        //
        $a7 = $xml->createElement("PorcentajeRetencion");
        $porcentaje = $xml->createTextNode($row['Porcentaje']);
        $a7->appendChild($porcentaje);
        //
        $book = $xml->createElement("DetalleRetencion");
        $book->appendChild($a1);
        $book->appendChild($a2);
        $book->appendChild($a3);
        $book->appendChild($a4);
        $book->appendChild($a5);
        $book->appendChild($a6);
        $book->appendChild($a7);
        $root->appendChild($book);
    }
    $xml->formatOutput = true;
    $name = strftime('backup_%m_%d_%Y.xml');
    header('Content-Disposition: attachment;filename=' . $name);
    header('Content-Type: text/xml');
    $xml->save('php://output');
//  echo $xml->saveXML();
//  $xml->save("mybooks.xml") or die("Error");
}

我在谷歌上搜索了很多,但我只能找到那些真正想将HTML代码输出到xml 中的人

不能对单个http请求输出两个响应。

就像图像或下载一样,您需要一个单独的请求,该请求只输出XML,并为用户提供调用它的方法(Link、Form、Redirect、Javascript)。