gpt4 book ai didi

php - 使用 PHP 获取原始请求

转载 作者:可可西里 更新时间:2023-10-31 22:42:06 25 4
gpt4 key购买 nike

我正在寻找一种方法,使用 native PHP 获取我的脚本收到的原始 HTTP 请求,包括 header 和正文。阅读 PHP 文档,无论使用何种 HTTP 方法,我都找不到获取原始请求的标准方法。

例如,当请求页面 test.php 时,我想获取完整的请求,如下所示:

GET /test.php HTTP/1.1
Host:....
....
....

在 POST、HEAD 等情况下也是如此......

似乎很奇怪,不存在访问原始请求缓冲区的方法!

最佳答案

查看手册似乎没有未解析的原始访问请求以匹配您想要的内容,因此我怀疑您需要从 $_SERVER 重新构建您想要的内容> 变量。快速搜索我找到了这个类,做了一些小改动以获得 GET/HTTP/1.1,也许你会发现它适合你的需要。

<?php
/**
* Access the HTTP Request
*
* Found on http://www.daniweb.com/web-development/php/code/216846/get-http-request-headers-and-body
*/
class http_request {

/** additional HTTP headers not prefixed with HTTP_ in $_SERVER superglobal */
public $add_headers = array('CONTENT_TYPE', 'CONTENT_LENGTH');

/**
* Construtor
* Retrieve HTTP Body
* @param Array Additional Headers to retrieve
*/
function http_request($add_headers = false) {

$this->retrieve_headers($add_headers);
$this->body = @file_get_contents('php://input');
}

/**
* Retrieve the HTTP request headers from the $_SERVER superglobal
* @param Array Additional Headers to retrieve
*/
function retrieve_headers($add_headers = false) {

if ($add_headers) {
$this->add_headers = array_merge($this->add_headers, $add_headers);
}

if (isset($_SERVER['HTTP_METHOD'])) {
$this->method = $_SERVER['HTTP_METHOD'];
unset($_SERVER['HTTP_METHOD']);
} else {
$this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;
}
$this->protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : false;
$this->request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;

$this->headers = array();
foreach($_SERVER as $i=>$val) {
if (strpos($i, 'HTTP_') === 0 || in_array($i, $this->add_headers)) {
$name = str_replace(array('HTTP_', '_'), array('', '-'), $i);
$this->headers[$name] = $val;
}
}
}

/**
* Retrieve HTTP Method
*/
function method() {
return $this->method;
}

/**
* Retrieve HTTP Body
*/
function body() {
return $this->body;
}

/**
* Retrieve an HTTP Header
* @param string Case-Insensitive HTTP Header Name (eg: "User-Agent")
*/
function header($name) {
$name = strtoupper($name);
return isset($this->headers[$name]) ? $this->headers[$name] : false;
}

/**
* Retrieve all HTTP Headers
* @return array HTTP Headers
*/
function headers() {
return $this->headers;
}

/**
* Return Raw HTTP Request (note: This is incomplete)
* @param bool ReBuild the Raw HTTP Request
*/
function raw($refresh = false) {
if (isset($this->raw) && !$refresh) {
return $this->raw; // return cached
}
$headers = $this->headers();
$this->raw = "{$this->method} {$_SERVER['REQUEST_URI']} {$this->protocol}\r\n";

foreach($headers as $i=>$header) {
$this->raw .= "$i: $header\r\n";
}
$this->raw .= "\r\n{$this->body}";
return $this->raw;
}

}

/**
* Example Usage
* Echos the HTTP Request back to the client/browser
*/
$http_request = new http_request();

$resp = $http_request->raw();

echo nl2br($resp);
/* Result (less <br> tags)
GET / HTTP/1.1
HOST: localhost:8080
USER-AGENT: Mozilla/5.0 ...
ACCEPT: text/html,application/xhtml+xml,application/xml;...
ACCEPT-LANGUAGE: en-US,en;q=0.5
ACCEPT-ENCODING: gzip, deflate
DNT: 1
COOKIE: PHPSESSID=...
CONNECTION: keep-alive
*/
?>

P.S: 不要忘记 htmlentities() 它们输出值 :)

关于php - 使用 PHP 获取原始请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23446989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com