我想知道是否可以在发送响应之前将整个 http 响应保存在服务器端的某些 php 脚本末尾?如果我能以十六进制格式保存响应会更好吗? 或者我可以在阿帕奇日志中看到它吗?
非常感谢
编辑:
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;
}
而且我想保存包含标题的整个响应,我不知道,例如:
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();
?>
在脚本的开头放:
ob_start();
并把它放在脚本的末尾
$output = ob_get_clean();
echo $output;
file_put_contents(time().'output.txt', $output);