$http_response_header 用法

本文深入探讨PHP中$http_response_header变量的用途及其实现原理,解析其与get_headers()函数的相似之处,以及在使用HTTP包装器时如何通过fopen、file_get_contents和copy函数获取远程文件的HTTP响应头。
今天在处理 '如何得到本地或远程文件的 MIME 类型' 问题时,了解到了 '$http_response_header',之前可能也见到过,但一定是完全没注意过,没想到还有点意思,记录下,也给大家分享下这个知识点。

$http_response_header 

首先看官方文档:
	https://www.php.net/manual/zh/reserved.variables.httpresponseheader.php

	同我们熟知的 $GLOBALS, $_SERVER, $_GET ... 都是 '预定义变量'(正好我们也可以仔细看下 PHP 所有的预定义变量,以及其他几个特殊的预定义变量的用法)

	查看说明,我们一句一句分析:
		1.$http_response_header 数组与 'get_headers()' 函数类似
			我们来看下 get_headers() 函数:
				get_headers($url[, $format = 0]) - 请求一个 url,不获取内容,只得到 HTTP 响应头

					$format 参数,只是返回不同格式的数组:
						0 - [
								[0] => HTTP/1.1 200 OK
								[1] => Content-Type: text/html
								...
							]

						1 - [
								[0] => HTTP/1.1 200 OK
								[Content-Type] => text/html
								...
							]

				所以我们知道,get_headers() 就是只获取远程地址 HTTP 响应头

		2.当使用 'HTTP 包装器' 时,$http_response_header 将会被 HTTP 响应头信息填充。
			HTTP 包装器是什么,我们继续查看文档(这部分属于 '支持的协议和封装协议'):
				http 和 https 协议 - 就是访问远程的 http/https 网址
				只要在 PHP 内,调用内置的部分函数请求了远程地址,就会自动将 HTTP 响应头填充到 $http_response_header 变量中

				哪些函数调用可以得到 $http_response_header,目前了解到的有:
					fopen
					file_get_contents
					copy(远程文件地址, 本地系统文件地址)
				注意 curl 扩展,不会得到 $http_response_header

测试示例:
	1.fopen()
		$url = 'http://www.xxx.com/a.jpg';
		fopen($url, 'r');
		var_dump($http_response_header);


	2.file_get_contents()
		$url = 'http://www.xxx.com/a.jpg';
		file_get_contents($url);
		var_dump($http_response_header);

	3.copy()
		$url = 'http://www.xxx.com/a.jpg';
		copy($url, $local_destination_path);
		var_dump($http_response_header);

 

Check prerequisites specific to the Nginx integration For this integration to work, you must first configure a custom JSON access log in the NGINX configuration, as well as enable the Stub Status Module and configure the nginx-prometheus-exporter. First, to enable the logging, place the following configuration within the http block, by the # Logging Settings line: log_format json_analytics escape=json '{' '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution '"connection": "$connection", ' # connection serial number '"connection_requests": "$connection_requests", ' # number of requests made in connection '"pid": "$pid", ' # process pid '"request_id": "$request_id", ' # the unique request id '"request_length": "$request_length", ' # request length (including headers and body) '"remote_addr": "$remote_addr", ' # client IP '"remote_user": "$remote_user", ' # client HTTP username '"remote_port": "$remote_port", ' # client port '"time_local": "$time_local", ' '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format '"request": "$request", ' # full path no arguments if the request '"request_uri": "$request_uri", ' # full path and arguments if the request '"args": "$args", ' # args '"status": "$status", ' # response status code '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client '"http_referer": "$http_referer", ' # HTTP referer '"http_user_agent": "$http_user_agent", ' # user agent '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for '"http_host": "$http_host", ' # the request Host: header '"server_name": "$server_name", ' # the name of the vhost serving the request '"request_time": "$request_time", ' # request processing time in seconds with msec resolution '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers '"upstream_response_time": "$upstream_response_time", ' # time spent receiving upstream body '"upstream_response_length": "$upstream_response_length", ' # upstream response length '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable '"ssl_protocol": "$ssl_protocol", ' # TLS protocol '"ssl_cipher": "$ssl_cipher", ' # TLS cipher '"scheme": "$scheme", ' # http or https '"request_method": "$request_method", ' # request method '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0 '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise '"gzip_ratio": "$gzip_ratio"' '}'; access_log /var/log/nginx/json_access.log json_analytics; """ where i should place this, this is the tutorial when i setting up prometheus , grafana alloy and nginx
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值