How to Display External XML file with PHP?

As the title says… I can do this with ASP but not sure how to code this for php?

go to this link

i think this is what u r looking for

I’m not using flash, I just want to know how to use php to display xml from another site.

The PHP documentation has lots on XML. The only problem I can see that you might have with external files is that you may have to use cURL to get them.

i wrote this code, it maps an XML file into an array. The only thing it can’t cope with is two elements of the same name in the same parent (so poss not useful). however this can be solved by getting the attribute rather than the tagName.

function xmlArray($file) {
	$doc = new domDocument();
	$doc->load($file);
	if($doc) {
		$library = $doc->documentElement;
		$shelves = $library->childNodes;
		foreach ($shelves as $shelf) {
			if ($shelf instanceof domElement) {
				$xa[$shelf->tagName] = array();
				$books = $shelf->childNodes;
				foreach ($books as $book) {
					if ($book instanceof domElement) {
						$xa[$shelf->tagName][$book->tagName] = $book->nodeValue;
					}
				}
			}
		}
		return $xa;
	} else {
		return false;
	}
}

However not sure if that will work well for external files.

I do know that simplexml works for external files, but is PHP 5 only:

// taken direct from PHP manual...
if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');

    var_dump($xml);
} else {
    exit('Failed to open test.xml.');
}

that works fine, returns an object map. hope this helps.

1 Like

using
join(“”,file(“filename.xml”));

and
explode();

is also a viable solution for a quick and easy kind of thing.

hi, I tried these scripts but each time I got an error: “Failed to open myxmlfile.xml”.

I then found this script below and ran it on my local server (php5) and it didn’t display the xml but then I ran it on my live host (php 4) and the xml was shown. Maybe this will help you understand more?:

<?php

$URL = “myxmlfile.xml”;

$PageURL = urlencode($_SERVER[‘SCRIPT_NAME’].‘?’.$_SERVER[‘QUERY_STRING’]);

$goldencanurl=$URL.‘&PageURL=’.$PageURL;

// create a new CURL resource

$ch = curl_init();

// set URL and other appropriate options

curl_setopt($ch, CURLOPT_URL, “$goldencanurl”);

curl_setopt($ch, CURLOPT_HEADER, false);

// grab URL and pass it to the browser

curl_exec($ch);

// close CURL resource, and free up system resources

curl_close($ch);

?>

*myxmlfile.xml is used in place of real file name here

What I’m trying to achieve at the end of all this is have some simple php code that will take an external xml file and I can use xml commands to format it and use css to display it a certain way on my own web page. (I’m not that experienced at php but can do the same thing in ASP).

Since XML is all about making documents global then I’m surprised how little info there is on this subject or at least clear tutorials etc. I also find plenty of info on doing this for ASP & Javascript but very little info for Php which surprises me.

then do it in ASP.

penguin, I want to do it in Php for several reasons and the first is the hosting account is for php.

Would it help if I posted the code for the ASP page?

I found what I was looking for here: http://www.utexas.edu/learn/rss/includes.html

but for some reason the url I have for the xml file won’t work with it so I will check out that now…

You could try adding this to your “other appropriate options” code

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);