php获取指定url请求头,【PHP】获取浏览器HTTP请求header信息、获取服务器HTTP响应header信息...

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:19   2568   0

一、认识HTTP

HTTP协议历史及设计思路(点击浏览)二、获取浏览器HTTP请求header信息

1. Apach服务器下可以直接使用 PHP自带函数获取客户端HTTP请求头信息/*

作用:获取客户端HTTP请求所有头信息(header)

参数:无。

返回:HTTP请求所有头信息数组

*/

getallheaders()

实例:<?php

var_dump(getallheaders());

==>输出array(12) {

["Content-Type"] => string(0) ""

["Content-Length"] => string(1) "0"

["X-Original-Url"] => string(21) "/Home/Other/getHeader"

["Upgrade-Insecure-Requests"] => string(1) "1"

["User-Agent"] => string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36"

["Host"] => string(17) "www.example.com"

["Cookie"] => string(36) "PHPSESSID=7rjh2uomb8477dggmr85bg9067"

["Accept-Language"] => string(14) "zh-CN,zh;q=0.9"

["Accept-Encoding"] => string(13) "gzip, deflate"

["Accept"] => string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"

["Connection"] => string(10) "keep-alive"

["Cache-Control"] => string(9) "max-age=0"

}

2. 非apach环境下,可根据服务器$_SERVER信息获取HTTP请求的header信息,设计函数:function getHeader() {

$headers = array();

foreach ($_SERVER as $key => $value) {

if ('HTTP_' == substr($key, 0, 5)) {

$headers[str_replace('_', '-', substr($key, 5))] = $value;

}

if (isset($_SERVER['PHP_AUTH_DIGEST'])) {

$header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST'];

} elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {

$header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW']);

}

if (isset($_SERVER['CONTENT_LENGTH'])) {

$header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];

}

if (isset($_SERVER['CONTENT_TYPE'])) {

$header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];

}

}

return $headers;

}

实例:<?php

var_dump(getHeader());

==>输出array(11) {

["X-ORIGINAL-URL"] => string(21) "/Home/Other/getHeader"

["UPGRADE-INSECURE-REQUESTS"] => string(1) "1"

["USER-AGENT"] => string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36"

["HOST"] => string(17) "www.example.com"

["COOKIE"] => string(36) "PHPSESSID=7rjh2uomb8477dggmr85bg9067"

["ACCEPT-LANGUAGE"] => string(14) "zh-CN,zh;q=0.9"

["ACCEPT-ENCODING"] => string(13) "gzip, deflate"

["ACCEPT"] => string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"

["CONTENT-LENGTH"] => string(1) "0"

["CONNECTION"] => string(10) "keep-alive"

["CACHE-CONTROL"] => string(9) "max-age=0"

}

三、获取服务器HTTP响应header信息/*

作用:获取服务器响应一个 HTTP 请求所发送的所有标头

参数:

url:目标 URL;

format:如果将可选的 format 参数设为 1,则 get_headers() 会解析相应的信息并设定数组的键名。

返回:返回包含有服务器响应一个 HTTP 请求所发送标头的索引或关联数组,如果失败则返回 FALSE。

*/

get_headers ( string $url [,int $format = 0 ] )

实例:<?php

$url = 'http://www.example.com';

print_r(get_headers($url));

print_r(get_headers($url, 1));

?>

==>输出Array

(

[0] => HTTP/1.1 200 OK

[1] => Date: Sat, 29 May 2004 12:28:13 GMT

[2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)

[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT

[4] => ETag: "3f80f-1b6-3e1cb03b"

[5] => Accept-Ranges: bytes

[6] => Content-Length: 438

[7] => Connection: close

[8] => Content-Type: text/html

)

Array

(

[0] => HTTP/1.1 200 OK

[Date] => Sat, 29 May 2004 12:28:14 GMT

[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)

[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT

[ETag] => "3f80f-1b6-3e1cb03b"

[Accept-Ranges] => bytes

[Content-Length] => 438

[Connection] => close

[Content-Type] => text/html

)

四,参考资料

1. https://www.yiibai.com/manual/php/function.get-headers.html

2. https://www.oschina.net/question/54100_38761

3. https://blog..net/xuezhiwu001/article/details/61203045

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP