问题描述
其实我有两个问题.
(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.
这篇关于仅通过 curl 在 php 中检索标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!