简体   繁体   中英

Output PHP response as XML

Newbie here. I have AJAX requesting XML data. Since it's cross-domain, it's going through a PHP proxy. The issue is that the proxy returns the XML in the form of a string. This makes it hard to parse in Javascript. How can I 1) have the PHP return the data in the form of an XML object or 2) convert the string to XML after it is returned?

$.ajax({
url: 'proxy.php',
data: {requrl: request + '&reportType=' + report}
    })
   .done(function(response) {
   ...
   }

proxy.php:

<?php
$file = file_get_contents($_GET['requrl']);
echo $file;
?>

You can load XML from a string using the simplexml_load_string() function:

$file = file_get_contents($_GET['requrl']);
$xml = simplexml_load_string($string);

Or directly load the XML file into the string, like so:

$xml = simplexml_load_file($_GET['requrl']);

You can return the xml directly like so...

header('Content-type: text/xml; charset=utf-8');
echo $file;

If you want to parse the xml with php look into SimpleXML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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