简体   繁体   English

是否可以仅发送http请求以获取上次修改时间?

[英]Is it possible to send a http request only to get the last modified time?

Instead of the file itself? 而不是文件本身?

EDIT 编辑

Best with a demo in PHP? 最适合使用PHP进行演示?

Yes. 是。 The "HEAD" method returns only the response headers and not the actual data. “ HEAD”方法仅返回响应头,而不返回实际数据。

You can use php's get_headers function 您可以使用php的get_headers函数

$a = get_headers('http://sstatic.net/so/img/logo.png');
print_r($a);

In your HTTP request you should add any of these header attributes, and you may receive an 304 (Last modified) 在HTTP请求中,您应该添加以下任何标头属性,并且您可能会收到304(最后修改)

  1. If-Modified-Since 如果自修改
  2. If-None-Match 如果不匹配
  3. If-Unmodified-Since 如果未修改,自

Andrei is correct, HEAD will only get the headers. Andrei是正确的,HEAD将仅获得标头。 My suggestion will return only the header and no body if the condictions are met. 如果符合条件,我的建议将仅返回标题,不返回正文。 If the content has been updated the body will contain the new information. 如果内容已更新,则正文将包含新信息。

<?php
// Get Headers
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,            'http://example.com/'); 
curl_setopt($ch, CURLOPT_HEADER,         true); 
curl_setopt($ch, CURLOPT_NOBODY,         true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT,        10); 
$response = curl_exec($ch); 

// Process Headers
$headerLines = explode("\r\n", $response);
foreach ($headerLines as $headerLine) {
    $headerLineParts = explode(': ', $headerLine);
    if (count($headerLineParts) >= 2) {
        $headers[$headerLineParts[0]] = $headerLineParts[1];
    }
}

echo $headers['Last-Modified'];
?>

您需要编写一些服务来处理http请求,并提取所请求文件的最后修改日期,然后返回该日期作为响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM