Created
October 1, 2018 10:55
-
-
Save phpfour/8e343fb84adc160d23270436020cb81f to your computer and use it in GitHub Desktop.
Debug Incoming HTTP Requests
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 | |
function debugHttpRequest($filename, $mode = FILE_APPEND, $json = false) | |
{ | |
$output = '[' . date('Y-m-d H:i:s') . '] ' | |
. $_SERVER['REQUEST_METHOD'] . ' ' | |
. $_SERVER['REQUEST_URI'] | |
. "\n\n"; | |
foreach ($_SERVER as $key => $value) { | |
$header = ''; | |
if (strpos($key, 'HTTP_') === 0 || in_array($key, ['CONTENT_TYPE'])) { | |
$chunks = explode('_', $key); | |
$start = (strpos($key, 'HTTP_') === 0) ? 1 : 0; | |
for ($i = $start; $y = sizeof($chunks) - 1, $i < $y; $i++) { | |
$header .= ucfirst(strtolower($chunks[$i])).'-'; | |
} | |
$header .= ucfirst(strtolower($chunks[$i])).': '.$value; | |
$output .= $header."\n"; | |
} | |
} | |
$body = file_get_contents('php://input'); | |
if ($body != '') { | |
if ($json || (isset($_SERVER['CONTENT_TYPE']) && stripos($_SERVER['CONTENT_TYPE'], 'json') !== false)) { | |
if (json_decode($body) !== null) { | |
$output .= "\n" . $body . "\n\n"; | |
} else { | |
parse_str($body, $data); | |
if (count($data) > 0) { | |
$output .= "\n" . json_encode($data, JSON_PRETTY_PRINT) . "\n\n"; | |
} else { | |
$output .= "\n" . $body . "\n\n"; | |
} | |
} | |
} else { | |
$output .= "\n" . $body . "\n\n"; | |
} | |
} else { | |
$output .= "\n\n"; | |
} | |
file_put_contents($filename, $output, $mode); | |
} |
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 | |
require __DIR__.'/debug.php'; | |
debugHttpRequest('/tmp/req.log', FILE_APPEND, true); | |
/** FRAMEWORK STUFF BELOW */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment