I have an xml page which is constantly updating at a remote site - http://stats.enemyterritory.com/clan/341?xml=true

How do I embed the contents of that page (displayed as at http://stats.enemyterritory.com/clan/341) in a page on my drupal site?

Comments

dman’s picture

You need certain permissions on the server to get it to go and read other internet sites, but assuming you are allowed to do that, The tidiest method is file_get_contents()

print(file_get_contents("http://stats.enemyterritory.com/clan/341?xml=true"));

It's not the most efficient, as it will make your page load up to 3x slower every time (you certainly don't want to use this code in a block) and you should instead consider caching at your end, but there are trade-offs whatever you choose.

If you do NOT have the ability to read remote sites from your server via PHP, (due to hosting restrictions) work-arounds are probably possible, but you have to learn a lot about HTTP transactions to create a handmade solution. Either that or use an ugly IFRAME, depending totally on what you are trying to do.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

fuzzy_texan’s picture

Thanks for the reply. Unfortunately it looks like our hosting doesn't allow this. I'll look into alternatives.

mooffie’s picture

You can still fetch this remote file: use drupal_http_request() instead of file_get_contents():

$datefetch = drupal_http_request('http://......');
if ($datefetch->error) {
  drupal_set_message('Request error: '. $icaldatafetch->error, 'error');
  return;
}
$date = $datefetch->data;

The data is in XML, so you'll have to parse it. If you're using PHP 5 then you have the SimpleXML extension.

I see the table is sortable. You may wish to insert the data into a [temporary?] table and use Drupal's intrinsic ability to sort tables. Or you may use some jQuery plugin for that.

Another option is to ask the author of tablemanager to add an XML import feature.

mskristinahall’s picture

Hi.
I tried the small chunk of code you posted here and it did work better than any solution I have found thus far. Unfortunately it is showing a copy of the page itself in drupal when I simply want to display my guilds roster list in a customized way... Same will go with detailed character information. The guild roster list, I want to be done in a table sortable if I can find a way to do it.

I am making a World of Warcraft guild website and one of the things the guild needs is an always up to date guild roster. I want to use the information shown here http://www.wowarmory.com/guild-info.xml?r=Boulderfist&n=Forsaken+Deity&p... which when you view the source you can see it is XML...

I'm not sure how to go about making it work the way I want... if you know what I mean. I'm not exactly a wonderful programmer. I know bits and pieces enough to have rudimentary understanding of the code I am looking at... but not enough to be able to build something entirely from scratch.

I'm using hostmonster for my hosting which uses PHP version 5.1.6.

dman’s picture

Well, yes.
That line will give you the raw XML embedded as source- which is what you asked for.
For real display however, you'll most likely want to process that source in some way to render it in a nice HTML way. XML is XML, and not natively browser-friendly.

You can try to make it look like you want using CSS, but that's silly.
My preferred method is using XSL - which is how it's designed to be transformed ... but that requires a good understanding of the source data format, and a pretty steep learning curve.

The best way forward (to do things 'properly') is create an XSLT transformation template, and apply that to the retrieved data before rendering the result.
You can probably use some other XML libraries to parse the data into a PHP structure then print the results, but that also entails learning more than you want to about XML parsing.

No quick answer, unless you are prepared to put some effort into working with the protocols.

PS:
Looking at that link, it's a VERY clever use of XSL - they deserve an award for using it so well!
The XSL stylesheet is <?xml-stylesheet type="text/xsl" href="/layout/guild-info.xsl"?> and that's where you could begin to learn how they did it.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Zoologico’s picture

Did you ever get this to work like you needed?

I got only as far as displaying the actual XML, but not the data I need to get our of the XML file.

I used the examples above and tried to incorporate all the tips using this code:

$datafetch = drupal_http_request('file.xml');
if ($datafetch->error) {
  drupal_set_message('Request error: '. $icaldatafetch->error, 'error');
  return;
}
$data = $datafetch->data;

print simplexml_load_file($data);

I end up with a warning:

warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity

But the XML file does end up displaying the code but not the content.

Any more ideas?

summit’s picture

I have the same question.
May be it is possible with feedapi and a specific parser. Any Ideas?

greetings,
Martijn

russ77’s picture

Had it ever been resolved?

http://www.rssmedco.com

VVN’s picture

i'm using:

 $xml = new SimpleXMLElement($data); 
print $xml->section;

and works pretty good

russ77’s picture

I believe this is good only for PHP5. Anything for earlier versions of PHP?

http://www.rssmedco.com

takinola’s picture

You get the warning because simplexml_load_file() expects a file as argument but instead you are providing a string. If you have converted the file into a string array then you need to use simplexml_load_string() instead

The code should read

print simplexml_load_string($data)