头只通过curl在php中检索 [英] Header only retrieval in php via curl

查看:94
本文介绍了头只通过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 (curl句柄)。您可以通过将 CURLINFO_FILETIME 作为第二个参数传递给 curl_getinfo() / code>。 (通常文件时间不可用,在这种情况下它将被报告为-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.

这篇关于头只通过curl在php中检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆