Import a Remote File with PHP and XML

48
Article Source About GNU/Linux
October 18, 2009, 1:08 am

How to import the content from a remote file by using PHP and XML ?

That is very easy ! First, you must create the XML file (I give you a simple example, look for yourself to get the file you want/need) :

 <character>
<body>Hello world !</body>
</character>

Save this in a file (we call it file.xml).

Then, in your PHP file, you must have this :

$xml = simplexml_load_file('http://url.tld//your/file.xml');
echo 'What is there in the file ? '.$xml->body0.'<br />';
  • simplexml_load_file() is a function that calls the remote file in $xml ;
  • “echo” shows the content of <body> and </body> from the XML file ;
  • $xml->body[0] means that “echo” has to show the first body of the file, if you wish to show the second body, you should have $xml->body[1].

Finally, here is what you should get :

What is there in the file ? Hello world !

Easy no ?

It seems it’s not possible to import a remote file only with php, good for you, because this could be a vulnerability.