[英]How to save entire http response to log file on server side just before responsing to the client
I am wondering if it is possible to save the entire http response on the server side at the end of some php script just before sending reponse? 我想知道是否有可能在发送响应之前将整个http响应保存在某些php脚本末尾的服务器端? Even better if i can save response in hex format? 如果我可以将响应保存为十六进制格式,那就更好了? Or can i see it in apache log maybe? 或者我可以在Apache日志中看到它吗?
many thanks 非常感谢
Edit: 编辑:
function shutDownFunction() {
$error = error_get_last();
if ( !empty($error) ) {
header('HTTP/1.0 500' .$error );
} else {
header('HTTP/1.0 200');
return '<html> <body> ... </body></html>';
}
}
register_shutdown_function('shutdownFunction');
try {
$data = file_get_contents("php://input");
//do some work here
} catch (Exception $e) {
header('HTTP/1.0 503');
die;
}
And I want to save entire response inluding headers, i don-t know , for example : 我想保存包含标题的整个响应,例如,我不知道:
GET /tutorials/other/top-20-mysql-best-practices/ HTTP/1.1
Host: net.tutsplus.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120
Pragma: no-cache
Cache-Control: no-cache
<?php
function callback($buffer)
{
// Save the output (to append or create file)
$fh = fopen("out.txt", "a");
fwrite($fh, $buffer);
fclose($fh);
// Return the output
return $buffer;
}
// Any thing that is indended to be sent to the client is stored in a buffer and callback is called
ob_start("callback");
// Write out to buffer here
echo "TEST";
echo " SOME MORE DATA";
echo "\r\nNEW LINE";
echo "<b>SOME HTML</b>";
// Send to client
ob_end_flush();
?>
At te beggining of the script put: 在脚本开始处输入:
ob_start();
and put this at the end of script 并将其放在脚本的末尾
$output = ob_get_clean();
echo $output;
file_put_contents(time().'output.txt', $output);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.