tags:

views:

266

answers:

4

I have a simple code written (based on some tutorials found around the internet) to parse and display an XML file. However, I only know how to reference an XML file stored on my server and I would like to be able to use an XML file that is being returned to me from a POST.

Right now my code looks like this:

if( ! $xml = simplexml_load_file('test.xml') )
{
    echo 'unable to load XML file';
}
else
{
    foreach( $xml as $event)
    {
        echo 'Title: ';
        echo "<a href=\"$event->url\">$event->title</a><br />";
        echo 'Description: '.$event->info.'<br />';
        echo '<br />';
    }
} 

Is there some way I can replace the simpleXML_load_file function with one that will allow me to point to the POST URL that returns the XML file?

+3  A: 

Use simplexml_load_string instead of loadfile:

 simplexml_load_string($_POST['a']);

If you get the url to the file in the POST you can propably use the simplexml_load_file function with the url, but if that doesn't work you can use the file_get_contents in combination with the simplexml_load_string:

//say $_POST['a'] == 'http://example.com/test.xml';
simplexml_load_file($_POST['a']); // <-- propably works
simplexml_load_string(file_get_contents($_POST['a'])); //<-- defenitly works (propaly what happens internally)

also getting contents of external files could be prohibited by running PHP in safe mode.

Pim Jager
+2  A: 

If you are receiving a file that's been uploaded by the user, you can find it (the file) looking at the content of the $_FILES superglobal variable -- and you can read more about files uploads here (for instance, don't forget to call move_uploaded_file if you don't want the file to be deleted at the end of the request).

Then, you can work with this file the same way you already do with not-uploaded files.


If you are receiving an XML string, you can use simplexml_load_string on it.


And if you are only receiving the URL to a remote XML content, you have to :

  • download the file to your server
  • and, then, parse its content.

This can be done using simplexml_load_file, passing the URL as a parameter, if your server is properly configured (i.e. if allow_url_fopen is enabled).

Else, the download will have to be done using curl -- see curl_exec for a very basic example, and curl_setopt for the options you can use (you'll especially want to use CURLOPT_RETURNTRANSFER, to get the XML data as a string you can pass to simplexml_load_string).

Pascal MARTIN
A: 

Check out simplexml_load_string. You can then use cURL to do the post and fetch the result. An example:

<?php
$xml = simplexml_load_string($string_fetched_with_curl);
?>
Emil Vikström
A: 

From http://www.developershome.com/wap/wapUpload/wap_upload.asp?page=php4:

If you do not want to save the uploaded file directly but to process it, the PHP functions file_get_contents() and fread() can help you. The file_get_contents() function returns a string that contains all data of the uploaded file:

if (is_uploaded_file($_FILES['myFile']['tmp_name']))
  $fileData = file_get_contents($_FILES['myFile']['tmp_name']);

That will give you a handle on the raw text within that file. From there you will need to parse through the XML. Hope that helps!

Colin O'Dell