仅通过 curl 在 php 中检索标头

2022-01-11 00:00:00 header curl php

其实我有两个问题.

(1) 如果我只检索标题而不是使用 php 和 curl 检索整页,那么远程服务器上使用的 处理能力 或 带宽 是否会有所降低?

(1) Is there any reduction in processing power or bandwidth used on remote server if I retrieve only headers as opposed to full page retrieval using php and curl?

(2) 因为我认为,而且我可能错了,所以第一个问题的答案是 YES,我试图获取最后修改日期或仅远程文件的 If-Modified-Since 标头为了将它与本地存储的数据的时间日期进行比较,所以我可以在它被更改的情况下,将它存储在本地.但是,我的脚本似乎无法获取那条信息,当我运行它时,我得到 NULL:

(2) Since I think, and I might be wrong, that answer to first questions is YES, I am trying to get last modified date or If-Modified-Since header of remote file only in order to compare it with time-date of locally stored data, so I can, in case it has been changed, store it locally. However, my script seems unable to fetch that piece of info, I get NULL, when I run this:

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),即使我没有请求,也会显示标题数据,如下所示:

In case $header = curl_exec($curl) is uncomented, header data is displayed, even if I haven't requested it and is as follows:

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".

Based on that, 'Last-Modified' is returned.

那么,我做错了什么?

推荐答案

您将 $header 传递给 curl_getinfo().它应该是 $curl (卷曲句柄).您可以通过将 CURLINFO_FILETIME 作为第二个参数传递给 curl_getinfo() 来获取 filetime.(通常filetime不可用,此时会报-1).

You are passing $header to curl_getinfo(). It should be $curl (the curl handle). You can get just the filetime by passing CURLINFO_FILETIME as the second parameter to curl_getinfo(). (Often the filetime is unavailable, in which case it will be reported as -1).

不过,您的课程似乎很浪费,丢掉了很多可能有用的信息.这是另一种可能的完成方式:

Your class seems to be wasteful, though, throwing away a lot of information that could be useful. Here's another way it might be done:

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 请求).打火机多少会有很大差异.

Yes, the load will be lighter on the server, since it's only returning only the HTTP header (responding, after all, to a HEAD request). How much lighter will vary greatly.

相关文章