其实我有两个问题.
(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
}
}
Run Code Online (Sandbox Code Playgroud)
如果$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
Run Code Online (Sandbox Code Playgroud)
在此基础上,返回"Last-Modified".
那么,我做错了什么?
GZi*_*ipp 50
你正在传递$ 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';
}
Run Code Online (Sandbox Code Playgroud)
是的,服务器上的负载会更轻,因为它只返回HTTP标头(毕竟,响应HEAD请求).轻微多少会有很大差异.
pat*_*ick 38
为什么要使用CURL呢?有一个PHP函数:
$headers=get_headers("http://www.amazingjokes.com/img/2014/530c9613d29bd_CountvonCount.jpg");
print_r($headers);
Run Code Online (Sandbox Code Playgroud)
返回以下内容:
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
)
Run Code Online (Sandbox Code Playgroud)
在此之后应该很容易获得内容类型.
您还可以将format = 1添加到get_headers:
$headers=get_headers("http://www.amazingjokes.com/img/2014/530c9613d29bd_CountvonCount.jpg",1);
print_r($headers);
Run Code Online (Sandbox Code Playgroud)
这将返回以下内容:
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
)
Run Code Online (Sandbox Code Playgroud)
Ian*_*emp 15
(1)是的.HEAD请求(在这种情况下发出)在服务器上要轻得多,因为它只返回HTTP头,而不是像标准GET请求那样的头和内容.
(2)true在调用curl_exec()返回内容之前,需要将CURLOPT_RETURNTRANSFER选项设置为,而不是打印:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Run Code Online (Sandbox Code Playgroud)
这也应该让你的班级正常工作.
您可以设置默认流上下文:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
Run Code Online (Sandbox Code Playgroud)
然后使用:
$headers = get_headers($url,1);
Run Code Online (Sandbox Code Playgroud)
一旦 get_headers 跳过诸如登录提示或 cookie 等触发身份验证例程之类的步骤,get_headers 似乎比 cURL 更有效。
这是我使用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;
};
Run Code Online (Sandbox Code Playgroud)
用法示例:
$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'
)
Run Code Online (Sandbox Code Playgroud)