容错 file_get_contents

发布于 2025-01-08 05:56:55 字数 769 浏览 0 评论 0原文

我有一个具有以下架构的网站:

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

服务器 A 是一个简单的 Web 服务器,主要提供静态 HTML 页面。但是,某些内容是动态的,并且该内容是从服务器 B 获取的。示例:

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

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

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

这工作正常。但是,如果服务器 B 宕机(维护、意外崩溃等),服务器 A 上的那些动态页面将会失败。因此,我想

  • 缓存 file_get_contents 的最后结果,并
  • 在 file_get_contents 超时时显示此结果。

现在,实现这样的事情应该不会太难;然而,这似乎是一种常见的情况,我想避免重新发明轮子。是否有一些 PHP 库或内置功能可以帮助解决这种情况?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁杀 2025-01-15 05:56:55

我会做这样的事情:

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);
}

i would do something 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);
}
烦人精 2025-01-15 05:56:55

您可以检查文件的修改时间,只有当不同时才请求它,否则加载本地副本。另外,PHP 网站上的 filemtime 注释中有一个缓存伪示例(来自: 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);
}
?>

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);
}
?>
绝情姑娘 2025-01-15 05:56:55

我接受了 dom 的回答,因为这是最有帮助的。我最终使用了稍微不同的方法,因为我想考虑可以通过端口 80 访问服务器但其他一些问题阻止它提供请求的信息的情况。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文