PHP獲取http請求的頭信息實現(xiàn)步驟
更新時間:2012年12月16日 14:36:01 作者:
PHP如何獲取http請求頭信息,是一個急切解決而不知道如何抉擇的問題,本人搜集整理下,可供參考下
PHP手冊提供了現(xiàn)成的函數(shù):
getallheaders
(PHP 4, PHP 5)
getallheaders — Fetch all HTTP request headers
說明
array getallheaders ( void )
Fetches all HTTP headers from the current request.
This function is an alias for apache_request_headers(). Please read theapache_request_headers() documentation for more information on how this function works.
返回值
An associative array of all the HTTP headers in the current request, orFALSE on failure.
Example #1 getallheaders() example
<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
?>
不過這個函數(shù)只能在apache環(huán)境下使用,iis或者nginx并不支持,可以通過自定義函數(shù)實現(xiàn)
<?php
<SPAN class=html>if (!function_exists('getallheaders'))
{
function getallheaders()
{
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}</SPAN>
?>
好了,看看都打印出了啥吧
<?php
print_r(getallheaders());
獲得結(jié)果:
Array
(
[Accept] => */*
[Accept-Language] => zh-cn
[Accept-Encoding] => gzip, deflate
[User-Agent] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
[Host] => localhost
[Connection] => Keep-Alive
)
getallheaders
(PHP 4, PHP 5)
getallheaders — Fetch all HTTP request headers
說明
array getallheaders ( void )
Fetches all HTTP headers from the current request.
This function is an alias for apache_request_headers(). Please read theapache_request_headers() documentation for more information on how this function works.
返回值
An associative array of all the HTTP headers in the current request, orFALSE on failure.
Example #1 getallheaders() example
復(fù)制代碼 代碼如下:
<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
?>
不過這個函數(shù)只能在apache環(huán)境下使用,iis或者nginx并不支持,可以通過自定義函數(shù)實現(xiàn)
復(fù)制代碼 代碼如下:
<?php
<SPAN class=html>if (!function_exists('getallheaders'))
{
function getallheaders()
{
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}</SPAN>
?>
好了,看看都打印出了啥吧
復(fù)制代碼 代碼如下:
<?php
print_r(getallheaders());
獲得結(jié)果:
復(fù)制代碼 代碼如下:
Array
(
[Accept] => */*
[Accept-Language] => zh-cn
[Accept-Encoding] => gzip, deflate
[User-Agent] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
[Host] => localhost
[Connection] => Keep-Alive
)
您可能感興趣的文章:
- php之curl實現(xiàn)http與https請求的方法
- PHP實現(xiàn)取得HTTP請求的原文
- php中調(diào)用其他系統(tǒng)http接口的方法說明
- PHP 使用header函數(shù)設(shè)置HTTP頭的示例解析 表頭
- php抓取https的內(nèi)容的代碼
- php curl 獲取https請求的2種方法
- 在PHP中實現(xiàn)使用Guzzle執(zhí)行POST和GET請求
- 在Laravel中使用GuzzleHttp調(diào)用第三方服務(wù)的API接口代碼
- 使用Zttp簡化Guzzle 調(diào)用
- PHP的HTTP客戶端Guzzle簡單使用方法分析
相關(guān)文章
php關(guān)聯(lián)數(shù)組快速排序的方法
這篇文章主要介紹了php關(guān)聯(lián)數(shù)組快速排序的方法,涉及php數(shù)組排序的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
php實現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài)
這篇文章主要介紹了php實現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài),Varnish是一款高性能的開源HTTP加速器,可以替代Squid、Nginx等服務(wù)器,需要的朋友可以參考下2014-12-12
php 的加密函數(shù) md5,crypt,base64_encode 等使用介紹
php 在做注冊、登錄或是url 傳遞參數(shù)時都會用到 字符變量的加密,下面我們就來簡單的介紹下:php 自帶的加密函數(shù)2012-04-04

