PHP远程文件最后修改时间
我想获取远程文件的最后修改时间。我正在使用我在 stackoverflow 上找到的这段代码
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
//don't fetch the actual page, you only want headers
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
//stop it from outputting stuff to stdout
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// attempt to retrieve the modification date
curl_setopt($curl, CURLOPT_FILETIME, true);
$result = curl_exec($curl);
echo $result;
$info = curl_getinfo($curl);
print_r($info);
if ($info['filetime'] != -1) { //otherwise unknown
echo date("Y-m-d H:i:s", $info['filetime']); //etc
}
这段代码的问题是我一直得到 filetime = -1 。但是当我删除时
curl_setopt($curl, CURLOPT_NOBODY, true);
,我会得到正确的修改时间。
是否可以获取最后修改时间但
curl_setopt($curl, CURLOPT_NOBODY, true);
包含在脚本中。 我只需要页面标题,而不是正文。
提前致谢
I want to get last modification time of the remote file. I am using this code I found here on stackoverflow
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
//don't fetch the actual page, you only want headers
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
//stop it from outputting stuff to stdout
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// attempt to retrieve the modification date
curl_setopt($curl, CURLOPT_FILETIME, true);
$result = curl_exec($curl);
echo $result;
$info = curl_getinfo($curl);
print_r($info);
if ($info['filetime'] != -1) { //otherwise unknown
echo date("Y-m-d H:i:s", $info['filetime']); //etc
}
Problem with this code I am getting filetime = -1 all the time. But when I delete
curl_setopt($curl, CURLOPT_NOBODY, true);
then I am getting correct modification time.
Is it possible to get last modification time but with
curl_setopt($curl, CURLOPT_NOBODY, true);
included in the script.
I just need header of the page, not body.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于我们的问答讨论中添加的信息,听起来您确实没有得到回复。可能是服务器配置了某种出于某种原因有意或无意地阻止 HEAD 请求的服务器,或者可能涉及到困难的代理。
当我调试 PHP cURL 内容时,我经常发现使用 *nix 盒子(我的 mac 或 ssh 到服务器)并从命令行运行请求非常有用,这样我就可以看到结果,而不必担心是否PHP 正在做正确的事情,直到我让 cURL 部分工作。例如:
Given the added information in our Q/A discussion, it sure sounds like you're just not getting a response. It could be that the server is configured with some sort of which intentionally or inadvertently blocks HEAD requests for some reason, or there could be a difficult proxy involved.
When I'm debugging PHP cURL stuff, I often find it useful to use a *nix box (my mac, or ssh to a server) and run the requests from the command line, so I can see the result without worrying about if the PHP is doing the right thing, until I get the cURL part working. For instance:
基于此解决方案
不下载文件的远程文件大小
Based on this solution
Remote file size without downloading file
我打算冒险说您正在连接的服务器可能是 IIS Web 服务器。
就我而言,我发现当我使用 PHP 通过 Curl 发出 HEAD 请求时,我连接到的 IIS 7 服务器不会返回上次修改日期(但在执行常规 GET 操作时它确实返回上次修改日期)要求)。
如果您可以控制要连接的服务器,请查看是否可以让 Web 服务器正确发出上次修改日期。否则不要使用 CURLOPT_NOBODY。
I'm going to take a punt and say that the server you're connecting to might be an IIS web server.
In my case, I've found that the IIS 7 server I'm connecting to does NOT return a Last-Modified date when I issue a HEAD request via Curl using PHP (but it does return the Last-Modified when doing a usual GET request).
If you are in control over the server you're connecting to, see if you can get the web server to correctly issue the Last-Modified date. Otherwise don't use CURLOPT_NOBODY.