使用PHP获取原始请求 [英] Get the raw request using PHP

查看:961
本文介绍了使用PHP获取原始请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to get, using native PHP, the raw HTTP request that my script received, including headers and body. Reading the PHP documentation I cant found a standard way to get the raw request, regardless the HTTP method used.

例如,当请求页面test.php时,我想得到完整的请求,如:

For example, when the page test.php is requested, I want to get the complete request like:

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

同样的情况POST,HEAD等......

The same in case of POST, HEAD, etc...

似乎很奇怪,没有一种方法可以访问原始请求缓冲区!

Seems very strange that there doesn't exist a method to access the raw request buffer!

推荐答案

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

Looking over the manual there doesn't seem to be an unparsed raw access to the request to match what you want, so I suspect you will need to re-build what you want from the $_SERVER variables. A quick search I found this class, made some small change to get the GET / HTTP/1.1, perhaps you will find it suits your needs.

<?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
*/
?>

PS:不要忘记输出上的htmlentities()值:)

P.S: Dont forget to htmlentities() them values on output :)

这篇关于使用PHP获取原始请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆