PHP獲取http請求的頭信息實(shí)現(xiàn)步驟
更新時(shí)間:2012年12月16日 14:36:01 作者:
PHP如何獲取http請求頭信息,是一個(gè)急切解決而不知道如何抉擇的問題,本人搜集整理下,可供參考下
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";
}
?>
不過這個(gè)函數(shù)只能在apache環(huán)境下使用,iis或者nginx并不支持,可以通過自定義函數(shù)實(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";
}
?>
不過這個(gè)函數(shù)只能在apache環(huán)境下使用,iis或者nginx并不支持,可以通過自定義函數(shù)實(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實(shí)現(xiàn)http與https請求的方法
- PHP實(shí)現(xiàn)取得HTTP請求的原文
- php中調(diào)用其他系統(tǒng)http接口的方法說明
- PHP 使用header函數(shù)設(shè)置HTTP頭的示例解析 表頭
- php抓取https的內(nèi)容的代碼
- php curl 獲取https請求的2種方法
- 在PHP中實(shí)現(xiàn)使用Guzzle執(zhí)行POST和GET請求
- 在Laravel中使用GuzzleHttp調(diào)用第三方服務(wù)的API接口代碼
- 使用Zttp簡化Guzzle 調(diào)用
- PHP的HTTP客戶端Guzzle簡單使用方法分析
相關(guān)文章
PHP實(shí)現(xiàn)異步調(diào)用方法研究與分享
瀏覽器和服務(wù)器之間只一種面向無連接的HTTP協(xié)議進(jìn)行通訊的,面向無連接的程序的特點(diǎn)是客戶端請求服務(wù)端,服務(wù)端根據(jù)請求輸出相應(yīng)的程序,不能保持持久連接2011-10-10PHP中sleep()函數(shù)的實(shí)用場景以及注意事項(xiàng)
sleep()函數(shù)是PHP中的一個(gè)休眠函數(shù),可以讓程序在指定的時(shí)間內(nèi)暫停執(zhí)行,以達(dá)到延遲執(zhí)行的效果,本文介紹使用sleep()函數(shù)的實(shí)用場景以及注意事項(xiàng)2023-09-09功能強(qiáng)大的PHP POST提交數(shù)據(jù)類
這篇文章主要為大家詳細(xì)介紹了功能強(qiáng)大的PHP POST提交數(shù)據(jù)類,代碼簡潔且具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07PHP對象轉(zhuǎn)換為數(shù)組函數(shù)(遞歸方法)
本方法主要是應(yīng)用于迭代對象。我應(yīng)用的地方是simplexml中的simplexml_load_string()上,因?yàn)榉祷氐娜菍ο?,如果提取?shù)據(jù)比較麻煩,所以應(yīng)用了下面的函數(shù)2012-02-02