简体   繁体   English

如何在响应客户端之前将整个http响应保存到服务器端的日志文件

[英]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.

相关问题 如何控制对 http 请求的响应的访问? (服务器端和客户端) - How to controll access for a response to a http request ? (server side & client side) HTTP响应是整个PHP文件 - HTTP response is entire PHP file Phalcon使用Incubator HTTP Client将zip文件保存到服务器 - Phalcon save zip file to server using Incubator HTTP Client 如何比较客户端时间和服务器端文件修改时间? - How to compare a client side time with a server side file modification time? 客户端生成 CSV 文件,使用 Angular POST 发送,通过 PHP 后端保存服务器端 - Generate CSV file client side, send using Angular POST, save server side through PHP backend 如何在客户端保存JSON文件并在以后检索? - How to save a JSON file on the client side and retrieve it later? 在客户端上调整图像大小,然后再上传到服务器 - Resize image on client side before uploading to the server 如何在发送到服务器端或客户端之前加密我网站上的数据? - How to encrypt data on my website before sending to server-side or client side? 如何在php查询之前启用日志 - How to enable a log just before the php query Ajax或jquery客户端裁剪和调整大小库,该库通过HTTP请求将裁剪后的文件发送到服务器 - Ajax or jquery client side crop and resize library which sends the cropped file to server with http request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM