PHP取得HTTP請求的原文

2021-06-23 06:06:17 字數 825 閱讀 4950

1. 取得請求行:method、uri、協議 

可以從超級變數$_server中獲得,三個變數的值如下: 

$_server['request_method'].' '.$_server['request_uri'].' '.$_server['server_protocol']."\r\n"; 

2. 取得所有header 

php有個內建函式getallheader(),是apache_request_headers()函式的乙個別名,可以將http請求的所有header以陣列形式返回。但這個函式只能工作在apache下,如果換了nginx或者命令列,會直接報函式不存在的錯誤。 

比較通用的方法是,從超級變數$_server中提取出來,有關header的鍵值都是「http_」開頭的,可以根據此特點取得所有的header。**如下: 

function get_all_headers()  

} return $headers; 

} 3. 取得body 

官方提供了一種獲取請求body的方法,即: 

file_get_contents('php://input') 

4. 最終** 

/** 

* 獲取http請求原文 

} // (3) 空行 

$raw .= "\r\n"; 

// (4) 請求body 

$raw .= file_get_contents('php://input'); 

return $raw; 

}

PHP取得HTTP請求的原文

1.取得請求行 method uri 協議 可以從超級變數 server中獲得,三個變數的值如下 server request method server request uri server server protocol r n 2.取得所有header php有個內建函式getallheade...

php獲取http請求原文

可以從超級變數 server中獲得,三個變數的值如下 server request method server request uri server server protocol r n php有個內建函式getallheader 是apache request headers 函式的乙個別名,可...

PHP 傳送HTTP請求

file get contents版本 傳送post請求 param string url 請求位址 param array post data post鍵值對資料 return string function send post url,post data 使用如下 post data array...