Last active
November 3, 2019 13:59
-
-
Save francescozanoni/eab23f286e6f950231d11ebb1b2c1ed4 to your computer and use it in GitHub Desktop.
[PHP] Get raw HTTP request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get raw HTTP request. | |
* | |
* Inspired by: | |
* - https://gist.github.com/magnetikonline/650e30e485c0f91f2f40 | |
* - https://www.php.net/manual/en/function.getallheaders.php#104307 | |
* | |
* @return string | |
*/ | |
function get_raw_http_request() { | |
// Request line | |
$data = sprintf( | |
"%s %s %s\r\n", | |
$_SERVER["REQUEST_METHOD"], | |
$_SERVER["REQUEST_URI"], | |
$_SERVER["SERVER_PROTOCOL"] | |
); | |
// Headers | |
foreach ($_SERVER as $name => $value) { | |
if (substr($name, 0, 5) === "HTTP_") { | |
$name = substr($name, 5); | |
$name = str_replace("_", " ", $name); | |
$name = strtolower($name); | |
$name = ucwords($name); | |
$name = str_replace(" ", "-", $name); | |
} else if ($name === "CONTENT_TYPE") { | |
$name = "Content-Type"; | |
} else if ($name === "CONTENT_LENGTH") { | |
$name = "Content-Length"; | |
} else { | |
continue; | |
} | |
$data .= ($name . ": " . $value . "\r\n"); | |
} | |
// Body | |
$data .= ("\r\n" . file_get_contents("php://input") . "\r\n"); | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment