Last active
August 29, 2015 14:26
-
-
Save Rican7/9dcbd6b7398698b55200 to your computer and use it in GitHub Desktop.
PHP HTTP Request Dumper
This file contains hidden or 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 | |
ini_set('error_reporting', E_ALL); | |
ini_set('display_errors', 1); | |
ini_set('log_errors', 1); | |
date_default_timezone_set('UTC'); | |
const LOG_PATH = '/var/log/debug/request-dump.log'; | |
// Combine header locations | |
$headers = array_merge($_ENV, $_SERVER); | |
ksort($headers); | |
// Open log | |
$log_stream = fopen(LOG_PATH, 'a'); | |
// Write access line | |
fwrite($log_stream, str_repeat('-', 80) . PHP_EOL); | |
fwrite( | |
$log_stream, | |
sprintf( | |
'LOG: %s - %s - [%s] "%s %s %s" %d "%s" "%s"', | |
isset($headers['REMOTE_ADDR']) ? $headers['REMOTE_ADDR'] : '', | |
isset($headers['PHP_AUTH_USER']) ? $headers['PHP_AUTH_USER'] : '', | |
date(DateTime::ISO8601), | |
isset($headers['REQUEST_METHOD']) ? $headers['REQUEST_METHOD'] : '', | |
isset($headers['REQUEST_URI']) ? $headers['REQUEST_URI'] : '', | |
isset($headers['SERVER_PROTOCOL']) ? $headers['SERVER_PROTOCOL'] : '', | |
isset($headers['REDIRECT_STATUS']) ? $headers['REDIRECT_STATUS'] : 200, | |
isset($headers['HTTP_REFERER']) ? $headers['HTTP_REFERER'] : '-', | |
isset($headers['HTTP_USER_AGENT']) ? $headers['HTTP_USER_AGENT'] : '-' | |
) . PHP_EOL . PHP_EOL | |
); | |
// Request line | |
fwrite( | |
$log_stream, | |
sprintf( | |
'%s %s %s', | |
isset($headers['REQUEST_METHOD']) ? $headers['REQUEST_METHOD'] : '', | |
isset($headers['REQUEST_URI']) ? $headers['REQUEST_URI'] : '', | |
isset($headers['SERVER_PROTOCOL']) ? $headers['SERVER_PROTOCOL'] : '' | |
) . PHP_EOL | |
); | |
// Host | |
fwrite($log_stream, (isset($headers['HTTP_HOST']) ? 'Host: '. $headers['HTTP_HOST'] : '') . PHP_EOL . PHP_EOL); | |
// Write headers | |
array_walk($headers, function ($val, $key) use ($log_stream) { | |
fwrite($log_stream, $key . ': ' . $val . PHP_EOL); | |
}); | |
fwrite($log_stream, PHP_EOL); | |
// Write body | |
stream_copy_to_stream(fopen('php://input', 'r'), $log_stream); | |
// Pad for the next request | |
fwrite($log_stream, PHP_EOL . PHP_EOL); | |
fclose($log_stream); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment