我想知道是否可以在发送响应之前在某些php脚本的末尾在服务器端保存整个http响应?如果我能以十六进制格式保存响应,那会更好吗?或者我可以在apache日志中看到它吗?
非常感谢编辑:
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
答案 0 :(得分:3)
<?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();
?>
答案 1 :(得分:2)
在脚本开头放:
ob_start();
并将其放在脚本
的末尾$output = ob_get_clean();
echo $output;
file_put_contents(time().'output.txt', $output);