Skip to content

Instantly share code, notes, and snippets.

@phpfour
Created October 1, 2018 10:55
Show Gist options
  • Save phpfour/8e343fb84adc160d23270436020cb81f to your computer and use it in GitHub Desktop.
Save phpfour/8e343fb84adc160d23270436020cb81f to your computer and use it in GitHub Desktop.
Debug Incoming HTTP Requests
<?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);
}
<?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