php 获取原始访问请求,使用PHP获取原始请求

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

/**

* 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
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:不要忘记ヶ辆()他们值输出:)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值