容错 file_get_contents
我有一个具有以下架构的网站:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会做这样的事情:
i would do something like this:
您可以检查文件的修改时间,只有当不同时才请求它,否则加载本地副本。另外,PHP 网站上的 filemtime 注释中有一个缓存伪示例(来自: http://php.net/manual/en/function.filemtime.php ):
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 ):
我接受了 dom 的回答,因为这是最有帮助的。我最终使用了稍微不同的方法,因为我想考虑可以通过端口 80 访问服务器但其他一些问题阻止它提供请求的信息的情况。
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.