Is it possible to get the text of the HTTP request?

0

HTTP / HTTPS requests are sent as text having the format:

In order:

  • The starting line with the HTTP method and the
  • Headers in the format chave: valor
  • A blank line
  • The request body
  • Is it possible to get the complete string for this request? Is it saved in some .txt file in the /tmp directory?

        
    asked by anonymous 30.10.2018 / 21:58

    1 answer

    0

    You can retrieve all this information in PHP. They are not available in a file as it was said by "please delete my account". In the image, you have a request via POST, the headers and in the body all the information sent.

    When requesting ...

    To retrieve the request type along with the headers you can do this:

    $string = $_SERVER['REQUEST_METHOD']."\n"; // pega o tipo de requisição
    
    # pega e armazena todos os cabeçalhos
    foreach(getallheaders() as $nome => $valor)
    $string .= $nome." : ".$valor."\n";
    

    It will look something like what's in your image.

    To get the body you can use $_REQUEST to get everything ($ _GET, $ _POST, $ _COOKIE) that was sent. It would look like this:

    $string = $_SERVER['REQUEST_METHOD']."\n";
    foreach(getallheaders() as $nome => $valor)
        $string .= $nome." : ".$valor."\n";
    
    $string .= "Body:\n";
    foreach($_REQUEST as $key => $value)
        $string .= $key." : ".$value."\n";
    

    In response ...

    In response, you can use http_response_code() , which when it is not informed in the parameter with the response type, returns the current response from the server.

    And to get all the headers sent by the server, you can use get_headers() . which even sends the status of the response.

    The body of the answer is with you ... It may be an html, xml, a "hello world!" anything displayed, or nothing. And you can recover in many ways as with file_get_contents() if this response has a status of 200, that is, if there are no errors.

        
    17.12.2018 / 03:25