PHP cURL - opt ifmodsice returns http code = 200



我正在尝试设置一个 curl 请求,以便仅在自我存储的时间戳以来修改了远程文件时才获取远程文件。

我想管理我的 cURL 请求的 http 代码,下面是一个示例。

我已经存储了上次下载文件 XX 的时间戳。 2014-12-08 06:56:03 .我的网址请求

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEVALUE, strtotime($timestamp));
curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
$res = curl_exec($ch);
$info = curl_getinfo($ch);

正如文档所说

CURLOPT_TIMECONDITION

如何对待CURLOPT_TIMEVALUE。仅当页面自 CURLOPT_TIMEVALUE 中指定的时间以来已修改时,才使用 CURL_TIMECOND_IFMODSINCE 返回页面。如果尚未修改,则返回"304 未修改"标头,假设CURLOPT_HEADER为 TRUE。使用CURL_TIMECOND_IFUNMODSINCE获得相反的效果。CURL_TIMECOND_IFMODSINCE是默认值。

因此,如果我的时间戳是2014-12-08 06:56:03并且远程文件的文件时间为2014-12-08 04:59:03它必须返回 http 代码 304,但我总是得到 http 代码 200。我误解了文档吗?

这些是以秒为单位的时间戳

  • 本地文件1418021941
  • 远程文件1418014742

这是上述 curl 请求的信息

array(26) {
  ["url"]=> string(32) "XXX"
  ["content_type"]=> string(24) "application/octet-stream"
  ["http_code"]=> int(200)
  ["header_size"]=> int(251)
  ["request_size"]=> int(113)
  ["filetime"]=> int(1418014742)
  ["ssl_verify_result"]=> int(0)
  ["redirect_count"]=> int(0)
  ["total_time"]=> float(0.100412)
  ["namelookup_time"]=> float(0.010285)
  ["connect_time"]=> float(0.05576)
  ["pretransfer_time"]=> float(0.055878)
  ["size_upload"]=> float(0)
  ["size_download"]=> float(0)
  ["speed_download"]=> float(0)
  ["speed_upload"]=> float(0)
  ["download_content_length"]=> float(371712)
  ["upload_content_length"]=> float(0)
  ["starttransfer_time"]=> float(0.100382)
  ["redirect_time"]=> float(0)
  ["redirect_url"]=> string(0) ""
  ["primary_ip"]=> string(11) "XXX"
  ["certinfo"]=> array(0) { }
  ["primary_port"]=> int(80)
  ["local_ip"]=> string(11) "XXX"
  ["local_port"]=> int(XX)
}

如果我将本地文件的时间戳更改为小于远程文件的时间戳,这些就是信息2014-12-06 06:56:03

array(26) {
  ["url"]=> string(32) "XXX"
  ["content_type"]=> string(24) "application/octet-stream"
  ["http_code"]=> int(200)
  ["header_size"]=> int(251)
  ["request_size"]=> int(113)
  ["filetime"]=> int(1418014742)
  ["ssl_verify_result"]=> int(0)
  ["redirect_count"]=> int(0)
  ["total_time"]=> float(0.583712)
  ["namelookup_time"]=> float(0.011975)
  ["connect_time"]=> float(0.056813)
  ["pretransfer_time"]=> float(0.056977)
  ["size_upload"]=> float(0)
  ["size_download"]=> float(371712)
  ["speed_download"]=> float(636807)
  ["speed_upload"]=> float(0)
  ["download_content_length"]=> float(371712)
  ["upload_content_length"]=> float(0)
  ["starttransfer_time"]=> float(0.103772)
  ["redirect_time"]=> float(0)
  ["redirect_url"]=> string(0) ""
  ["primary_ip"]=> string(11) "XXX"
  ["certinfo"]=> array(0) { }
  ["primary_port"]=> int(80)
  ["local_ip"]=> string(11) "XX"
  ["local_port"]=> int(XX)
}

如您所见,两者之间存在差异,[size download] ,第一个=0,第二个>0。

有什么建议吗?我总是会得到一个 http 代码 = 200?

---------编辑

这是我返回的带有本地时间戳的标头2014-12-08 06:59:01

HTTP/1.1 200 OK Server: nginx Date: Mon, 08 Dec 2014 10:23:06 GMT Content-Type: application/octet-stream Content-Length: 371712 Last-Modified: Mon, 08 Dec 2014 04:59:02 GMT Connection: keep-alive ETag: "54853016-5ac00" Accept-Ranges: bytes

是的,你误解了 dosc。

如果尚未修改,则假设 CURLOPT_HEADER 为 TRUE,将返回"304 未修改"标头。

无更改 -> 返回"304 未修改"标头。

修改 -> 返回 200

---------第二轮---------

文件最后修改日期: 星期四, 18 十二月 2014 05:37:48 GMT.

date_default_timezone_set("Etc/GMT");
$url = "...";
$timestamp = "2014-12-18 05:37:48";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEVALUE, strtotime($timestamp));
curl_setopt($ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
$res = curl_exec($ch);

1、如果我使用上面的测试代码,它会返回一个这样的头:

HTTP/1.1 304 Not Modified
Date: Thu, 18 Dec 2014 08:27:55 GMT
Server: Apache/2.4.7 (Ubuntu)
ETag: "17c-50a76fe108d7a"

2,当我更改$timestamp时,它将返回以下内容:

HTTP/1.1 200 OK
Date: Thu, 18 Dec 2014 08:31:02 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Thu, 18 Dec 2014 05:37:48 GMT
ETag: "17c-50a76fe108d7a"
Accept-Ranges: bytes
Content-Length: 380
Vary: Accept-Encoding
Content-Type: text/html
我认为您应该设置时区,

如果我不设置时区,它总是返回 200。

最新更新