Question

I have a website with the following architecture:

End user     --->    Server A (PHP)        --->        Server B (ASP.NET & Database)
             web                    file_get_contents
           browser                  

Server A is a simple web server, mostly serving static HTML pages. However, some content is dynamic, and this content is fetched from Server B. Example:

someDynamicPageOnServerA.php:
<html>
     ...static stuff...

     <?php echo file_get_contents("http://serverB/somePage.aspx?someParameter"); ?>

     ...more static stuff...
</html>

This works fine. However, if server B is down (maintainance, unexpected crash, etc.), those dynamic pages on server A will fail. Thus, I'd like to

  • cache the last result of file_get_contents and
  • show this result if file_get_contents timeouted.

Now, it shouldn't be too hard to implement something like this; however, this seems to be a common scenario and I'd like to avoid re-inventing the wheel. Is there some PHP library or built-in feature that helps which such a scenario?

Was it helpful?

Solution

i would do somethin like this:

function GetServerStatus($site, $port){
    $fp = @fsockopen($site, $port, $errno, $errstr, 2);
    if (!$fp) {
       return false;
    } else { 
       return true;
    }
}

$tempfile = '/some/temp/file/path.txt';

if(GetServerStatus('ServerB',80)){
     $content = file_get_contents("http://serverB/somePage.aspx?someParameter");
     file_put_contents($tempfile,$content);
     echo $content;
}else{
     echo file_get_contents($tempfile);
}

OTHER TIPS

You could check the modified time of the file and only request it when it is different, otherwise load the local copy. Also, there is a cache pseudo-example on the PHP website in the comments for filemtime ( from: http://php.net/manual/en/function.filemtime.php ):

<?php
$cache_file = 'URI to cache file';
$cache_life = '120'; //caching time, in seconds

$filemtime = @filemtime($cache_file);  // returns FALSE if file does not exist
if (!$filemtime or (time() - $filemtime >= $cache_life)){
    ob_start();
    resource_consuming_function();
    file_put_contents($cache_file,ob_get_flush());
}else{
    readfile($cache_file);
}
?>

I accepted dom's answer, since it was the most helpful one. I ended up using a slightly different approach, since I wanted to account for the situation where the server is reachable via port 80 but some other problem prevents it from serving the requested information.

function GetCachedText($url, $cachefile, $timeout) {
    $context = stream_context_create(array(
     'http' => array('timeout' => $timeout)));  // set (short) timeout

    $contents = file_get_contents($url, false, $context);

    $status = explode(" ", $http_response_header[0]); // e.g. HTTP/1.1 200 OK
    if ($contents === false || $status[1] != "200") {
        $contents = file_get_contents($cachefile); // load from cache
    } else {
        file_put_contents($cachefile, $contents);  // update cache
    }
    return $contents;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top