php的get_headers可以取得服务器响应一个 HTTP 请求所发送的所有标头。
实际案例:
$url = 'http://www.walk-sing.com';
var_dump(get_headers($url));
输出结果:
array (size=19)
0 => string 'HTTP/1.1 302 Moved Temporarily' (length=30)
1 => string 'Server: nginx/1.4.1' (length=19)
2 => string 'Date: Fri, 24 Jul 2015 10:54:02 GMT' (length=35)
3 => string 'Content-Type: text/html' (length=23)
4 => string 'Content-Length: 160' (length=19)
5 => string 'Connection: close' (length=17)
6 => string 'Location: http://www.qq.com/' (length=28)
7 => string 'HTTP/1.1 200 OK' (length=15)
8 => string 'Server: squid/3.4.1' (length=19)
9 => string 'Date: Fri, 24 Jul 2015 10:45:45 GMT' (length=35)
10 => string 'Content-Type: text/html; charset=GB2312' (length=39)
11 => string 'Connection: close' (length=17)
12 => string 'Vary: Accept-Encoding' (length=21)
13 => string 'Vary: Accept-Encoding' (length=21)
14 => string 'Expires: Fri, 24 Jul 2015 10:46:45 GMT' (length=38)
15 => string 'Cache-Control: max-age=60' (length=25)
16 => string 'Vary: Accept-Encoding' (length=21)
17 => string 'Vary: Accept-Encoding' (length=21)
18 => string 'X-Cache: HIT from shanghai.qq.com' (length=33)
实际使用案例:
//获取手游的大小
public function getAppSize($url)
{
if ($url) {
$r = get_headers($url, 1);
$l = $r['Content-Length'] / 1048576;
$a = round($l, 2);
return $a;
} else {
return '0';
}
}