Ben Davidow
Ben Davidow

Reputation: 1215

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;
?>

Upvotes: 1

Views: 1952

Answers (2)

Rob
Rob

Reputation: 12872

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

Upvotes: 1

anon
anon

Reputation:

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']);

Upvotes: 0

Related Questions