Search code examples
phpcachingsimplexml

Caching xml data from remote server


Let's say I'm using simpleXML to parse weather data from a remote server, and then the remote server crashes so I can no longer recover its live feeds but I don't want my users to get an error message either, how would I go about caching and continuing to display the last piece of data I got from the server before it crashed?

Let's say my xml looks like this:

<weather>
    <temperature c="25" f="77">
</weather>

How would I go about displaying the values "25" and "77" until I'm able to reestablish a connection with the remote server?

Apologies if my question isn't entirely clear... my knowledge of server-side technologies is very limited.


Solution

  • This isn't the best way, but here is one way you could do it:

    To save the information

    $file = 'temp_cache.php';
    // Open the file to get existing content
    $content = '<?php $c="25"; $f="77"; ?>';
    // Write the contents to the file
    file_put_contents($file, $content);
    

    To load it

    include_once('temp.php');
    

    By including the file, your $c and $f variables will be set unless you overwrite them.