我需要在PHP页面中包含一个HTML页面(由 ASP.net 生成)。
为此,我使用:
echo file_get_contents("http://example.com");
但是这样,每次打开页面时,我的服务器都需要下载页面。
我想添加一个缓存系统,但每次 example.com 内容更改时都需要刷新缓存。
检测内容是否更改而不下载整个页面的最佳方法(如果有)是什么?
这里是远程页面的 HTTP 标头:
HTTP/1.1 200 OK =>
Cache-Control => no-cache
Pragma => no-cache
Content-Length => 63648
Content-Type => text/html; charset=utf-8
Expires => -1
Server => Microsoft-IIS/7.5
Set-Cookie => ASP.NET_SessionId=xxxxxxxxxxxxxxxx; path=/; HttpOnly
X-Powered-By => ASP.NET
X-AspNet-Version => 4.0.30319
X-UA-Compatible => chrome=1
X-CID => 2-18
Date => Thu, 12 Sep 2013 08:54:59 GMT
Connection => close
另一个网站给了我这些:
Server Response HTTP/1.1 200 OK
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 65367
Content-Type: text/html; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
Set-Cookie: ARRSID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/;Domain=.example.com
Set-Cookie: ASP.NET_SessionId=xxxxxxxxxxxxxxxxxxx; path=/; HttpOnly
X-Powered-By: UrlRewriter.NET 2.0.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-UA-Compatible: chrome=1
X-Powered-By: ARR/2.5
X-Powered-By: ASP.NET
X-CID: 1-18
Date: Thu, 12 Sep 2013 08:56:03 GMT
假设您的服务器支持它,最好的方法是使用所述页面的标题。
具体来说,请检查 If-Modified-Since
,只要您的网络服务器支持它,它就可以满足您的需求。
此外,您可以检查 ETags 标头,该标头将为内容提供标识符。页面上的更改应更改标识符(通常使用的是页面生成的时间戳)。同样,这取决于服务器配置。
您可以使用 cURL 检索标头、重新加载文件或提供缓存版本,具体取决于
Last-Modified: Fri, 14 Sep 2012 21:51:00 GMT
页眉
我已经使用了@Prasanth的解决方案,但它只是一个评论,我不能设置为答案,所以我在这里写它。
如果他想在这里写答案,我会把它设置为解决方案。
有关服务器上的缓存,请参阅此处。 用于了解何时 更改后,您必须找到有关服务器的独特之处。喜欢 服务器是否有
content-length
标头?如果是这样,您可以知道 如果此值发生更改,页面将刷新。但如果该网站不是 完全在您的控制之中,或者您无法确切知道何时 页面已更改,您可能希望刷新缓存在 服务器时不时地,可能使用 cron 作业。编辑:也检查 如果服务器具有 Last-Modified 标头,如 Ruben 所说。
因此,检查content-length
就可以解决问题。
> int filemtime ( string $filename )
返回上次修改日期 - 如果它是在您的缓存时间之后 - 您可以重新加载页面,如果不是从缓存中获取它。