Php 仅当已修改时才获取卷曲文件

Php 仅当已修改时才获取卷曲文件,php,curl,file-get-contents,Php,Curl,File Get Contents,我如何理解在使用CURL打开流之前是否修改了文件 (然后我可以用文件获取内容打开它) 谢谢检查卷发信息\u文件时间: $ch = curl_init('http://www.mysite.com/index.php'); curl_setopt($ch, CURLOPT_FILETIME, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_NOBODY, true); $exec = c

我如何理解在使用CURL打开流之前是否修改了文件 (然后我可以用文件获取内容打开它)


谢谢

检查
卷发信息\u文件时间

$ch = curl_init('http://www.mysite.com/index.php');
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$exec = curl_exec($ch);

$fileTime = curl_getinfo($ch, CURLINFO_FILETIME);
if ($fileTime > -1) {
    echo date("Y-m-d H:i", $fileTime);
} 

首先尝试发送HEAD请求,以获取目标url的上次修改的
头,以便与缓存版本进行比较。此外,您还可以尝试使用
If Modified Since
头,在使用GET请求创建缓存版本时使用
头,以便另一方也可以使用
302 Not Modified
响应您

使用curl发送HEAD请求如下所示:

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);
$content = curl_exec($curl);
curl_close($curl)
if (preg_match('/last-modified:\s?(?<date>.+)\n/i', $content, $m)) {
    // the last-modified header is found
    if (filemtime('your-cached-version') >= strtotime($m['date'])) {
        // your cached version is newer or same age than the remote content, no re-fetch required
    }
}
$content
现在将包含返回的HTTP头,作为一个长字符串,您可以在其中查找
上次修改:
,如下所示:

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);
$content = curl_exec($curl);
curl_close($curl)
if (preg_match('/last-modified:\s?(?<date>.+)\n/i', $content, $m)) {
    // the last-modified header is found
    if (filemtime('your-cached-version') >= strtotime($m['date'])) {
        // your cached version is newer or same age than the remote content, no re-fetch required
    }
}
if(preg_match('/last modified:\s?(?。+)\n/i',$content,$m)){
//找到最后修改的标题
如果(filemtime('your-cached-version')>=strotime($m['date'])){
//您的缓存版本比远程内容更新或相同,无需重新获取
}
}

您也应该以同样的方式处理
expires
标题(从标题字符串中提取值,检查该值是否在将来)

如果您已经有文件副本,您可以使用rsync。太复杂了!不管怎样,谢谢它工作得很好,但我现在有另一个问题!我试图获取文件更改的日期,比如:我获取的日期1970-01-01 00:59:59当然错了!我怎么能做到?我想你做不到,因为那是流式广播。很高兴它帮助了您。如果服务器不想返回文件时间,您不能对此采取任何措施。在您的情况下,返回501错误。但是它可以通过
curl_setopt($ch,CURLOPT_HEADER,1)完成
和阅读其他网站上上次修改的
$exec
。抱歉,我不明白在这种情况下我是否可以做!我试图设置curl_setopt($ch,CURLOPT_HEADER,1);但是$exec告诉我:HTTP/1.0 501未实现是的,服务器不想返回任何东西(要么响应不正确,要么需要升级)。我是curl新手,所以我不明白!