其实我有两个问题.
(1)如果我只检索标题而不是使用php和curl进行完整页面检索,那么远程服务器上使用的处理能力或带宽是否会减少?
(2)由于我认为,我可能错了,第一个问题的答案是肯定的,我试图获取最后修改日期或If-Modified-Since标题的远程文件只是为了将它与时间日期进行比较本地存储的数据,所以我可以,如果它已被更改,将其存储在本地.但是,NULL
当我运行这个时,我的脚本似乎无法获取那条信息:
class last_change { public last_change; function set_last_change() { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "http://url/file.xml"); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_FILETIME, true); curl_setopt($curl, CURLOPT_NOBODY, true); // $header = curl_exec($curl); $this -> last_change = curl_getinfo($header); curl_close($curl); } function get_last_change() { return $this -> last_change['datetime']; // I have tested with Last-Modified & If-Modified-Since to no avail } }
如果$header = curl_exec($curl)
未启用,即使我没有请求它,也会显示标题数据,如下所示:
HTTP/1.1 200 OK Date: Fri, 04 Sep 2009 12:15:51 GMT Server: Apache/2.2.8 (Linux/SUSE) Last-Modified: Thu, 03 Sep 2009 12:46:54 GMT ETag: "198054-118c-472abc735ab80" Accept-Ranges: bytes Content-Length: 4492 Content-Type: text/xml
在此基础上,返回"Last-Modified".
那么,我做错了什么?
你正在传递$ header curl_getinfo()
.它应该是$curl
(卷曲手柄).您可以filetime
通过传递CURLINFO_FILETIME
第二个参数来获得curl_getinfo()
.(通常filetime
不可用,在这种情况下,它将被报告为-1).
但是,你的课程似乎很浪费,丢掉了许多可能有用的信息.这是另一种可能的方式:
class URIInfo { public $info; public $header; private $url; public function __construct($url) { $this->url = $url; $this->setData(); } public function setData() { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $this->url); curl_setopt($curl, CURLOPT_FILETIME, true); curl_setopt($curl, CURLOPT_NOBODY, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); $this->header = curl_exec($curl); $this->info = curl_getinfo($curl); curl_close($curl); } public function getFiletime() { return $this->info['filetime']; } // Other functions can be added to retrieve other information. } $uri_info = new URIInfo('http://www.codinghorror.com/blog/'); $filetime = $uri_info->getFiletime(); if ($filetime != -1) { echo date('Y-m-d H:i:s', $filetime); } else { echo 'filetime not available'; }
是的,服务器上的负载会更轻,因为它只返回HTTP标头(毕竟,响应HEAD
请求).轻微多少会有很大差异.
为什么要使用CURL呢?有一个PHP函数:
$headers=get_headers("http://www.amazingjokes.com/img/2014/530c9613d29bd_CountvonCount.jpg"); print_r($headers);
返回以下内容:
Array ( [0] => HTTP/1.1 200 OK [1] => Date: Tue, 11 Mar 2014 22:44:38 GMT [2] => Server: Apache [3] => Last-Modified: Tue, 25 Feb 2014 14:08:40 GMT [4] => ETag: "54e35e8-8873-4f33ba00673f4" [5] => Accept-Ranges: bytes [6] => Content-Length: 34931 [7] => Connection: close [8] => Content-Type: image/jpeg )
在此之后应该很容易获得内容类型.
您还可以将format = 1添加到get_headers:
$headers=get_headers("http://www.amazingjokes.com/img/2014/530c9613d29bd_CountvonCount.jpg",1); print_r($headers);
这将返回以下内容:
Array ( [0] => HTTP/1.1 200 OK [Date] => Tue, 11 Mar 2014 22:44:38 GMT [Server] => Apache [Last-Modified] => Tue, 25 Feb 2014 14:08:40 GMT [ETag] => "54e35e8-8873-4f33ba00673f4" [Accept-Ranges] => bytes [Content-Length] => 34931 [Connection] => close [Content-Type] => image/jpeg )
更多阅读(PHP.NET)
(1)是的.HEAD请求(在这种情况下发出)在服务器上要轻得多,因为它只返回HTTP头,而不是像标准GET请求那样的头和内容.
(2)true
在调用curl_exec()
返回内容之前,需要将CURLOPT_RETURNTRANSFER选项设置为,而不是打印:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
这也应该让你的班级正常工作.
这是我使用CURLOPT_HEADER的实现,然后将输出字符串解析为映射:
function http_headers($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); $headers = curl_exec($ch); curl_close($ch); $data = []; $headers = explode(PHP_EOL, $headers); foreach ($headers as $row) { $parts = explode(':', $row); if (count($parts) === 2) { $data[trim($parts[0])] = trim($parts[1]); } } return $data; };
用法示例:
$headers = http_headers('https://i.ytimg.com/vi_webp/g-dKXOlsf98/hqdefault.webp'); print_r($headers); Array ( ['Content-Type'] => 'image/webp' ['ETag'] => '1453807629' ['X-Content-Type-Options'] => 'nosniff' ['Server'] => 'sffe' ['Content-Length'] => 32958 ['X-XSS-Protection'] => '1; mode=block' ['Age'] => 11 ['Cache-Control'] => 'public, max-age=7200' )