头只能通过curl在php中检索

其实我有两个问题。

(1)如果我只检索标题而不是使用php和curl进行全页检索,那么在远程服务器上使用的处理能力带宽有没有减less?

(2)由于我认为,我可能是错误的,对第一个问题的答案是YES ,我试图得到最后的修改date或If-Modified-Since头文件只是为了比较它的时间date本地存储的数据,所以我可以,如果它已被改变,存储在本地。 但是,我的脚本似乎无法获取该信息,我得到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 (curl手柄)。 你可以通过传递CURLINFO_FILETIME作为curl_getinfo()的第二个参数来得到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('Ymd H:i:s', $filetime); } else { echo 'filetime not available'; } 

是的,服务器上的负载将会更轻,因为它只返回HTTP头(毕竟,响应HEAD请求)。 多less打火机差异很大。

为什么使用CURL呢? 有一个PHP的function:

 $headers=get_headers("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 ) 

此后应该很容易得到内容types。

您也可以将format = 1添加到get_headers:

 $headers=get_headers("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)在调用curl_exec()以返回内容之前,需要将CURLOPT_RETURNTRANSFER选项设置为true ,而不是打印:

 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

这也应该让你的课程正确工作。

你需要添加

 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

返回标题而不是打印它。

无论是只返回头文件在服务器上是轻的取决于正在运行的脚本,但通常会。

我想你也想要“文件时间”,而不是“date时间”。

您可以设置默认stream上下文:

 stream_context_set_default( array( 'http' => array( 'method' => 'HEAD' ) ) ); 

然后使用:

 $headers = get_headers($url,1); 

一旦get_headers跳过触发validation例程(如login提示或cookie)的步骤,get_headers似乎比cURL更有效。

这里是我的实现使用CURLOPT_HEADER,然后parsing输出string到地图:

 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' )