How to print all information from an HTTP request to the screen, in PHP

后端 未结 9 1909
温柔的废话
温柔的废话 2020-12-08 01:55

I need some PHP code that does a dump of all the information in an HTTP request, including headers and the contents of any information included in a POST request. Basically,

相关标签:
9条回答
  • 2020-12-08 02:15

    Putting together answers from Peter Bailey and Cmyker you get something like:

    <?php
    foreach ($_SERVER as $key => $value) {
        if (strpos($key, 'HTTP_') === 0) {
            $chunks = explode('_', $key);
            $header = '';
            for ($i = 1; $y = sizeof($chunks) - 1, $i < $y; $i++) {
                $header .= ucfirst(strtolower($chunks[$i])).'-';
            }
            $header .= ucfirst(strtolower($chunks[$i])).': '.$value;
            echo $header."\n";
        }
    }
    $body = file_get_contents('php://input');
    if ($body != '') {
      print("\n$body\n\n");
    }
    ?>
    

    which works with the php -S built-in webserver, which is quite a handy feature of PHP.

    0 讨论(0)
  • 2020-12-08 02:15

    If you want actual HTTP Headers (both request and response), give hurl.it a try.

    You can use the PHP command apache_request_headers() to get the request headers and apache_response_headers() to get the current response headers. Note that response can be changed later in the PHP script as long as content has not been served.

    0 讨论(0)
  • 2020-12-08 02:15

    file_get_contents('php://input') will not always work.

    I have a request with in the headers "content-length=735" and "php://input" is empty string. So depends on how good/valid the HTTP request is.

    0 讨论(0)
  • 2020-12-08 02:15

    in addition, you can use get_headers(). it doesn't depend on apache..

    print_r(get_headers());
    
    0 讨论(0)
  • 2020-12-08 02:22

    The problem was, in the URL i wrote http://my_domain instead of https://my_domain

    0 讨论(0)
  • 2020-12-08 02:24

    Nobody mentioned how to dump HTTP headers correctly under any circumstances.

    From CGI specification rfc3875, section 4.1.18:

    Meta-variables with names beginning with "HTTP_" contain values read from the client request header fields, if the protocol used is HTTP. The HTTP header field name is converted to upper case, has all occurrences of "-" replaced with "" and has "HTTP" prepended to give the meta-variable name.

    foreach ($_SERVER as $key => $value) {
        if (strpos($key, 'HTTP_') === 0) {
            $chunks = explode('_', $key);
            $header = '';
            for ($i = 1; $y = sizeof($chunks) - 1, $i < $y; $i++) {
                $header .= ucfirst(strtolower($chunks[$i])).'-';
            }
            $header .= ucfirst(strtolower($chunks[$i])).': '.$value;
            echo $header.'<br>';
        }
    }
    

    Details: http://cmyker.blogspot.com/2012/10/how-to-dump-http-headers-with-php.html

    0 讨论(0)
提交回复
热议问题