首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在响应客户端之前将整个http响应保存到服务器端的日志文件中

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

Stack Overflow用户
提问于 2012-06-08 03:55:00
回答 2查看 5.7K关注 0票数 1

我想知道是否有可能在发送响应之前将整个http响应保存在服务器端的某个php脚本的末尾?如果我可以将响应保存为十六进制格式,那会更好?或者我可以在apache日志中看到它?

非常感谢

编辑:

代码语言:javascript
复制
      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;
    }

我想保存包含头部的整个响应,我不知道,例如:

代码语言:javascript
复制
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
EN

Stack Overflow用户

回答已采纳

发布于 2012-06-08 04:00:42

代码语言:javascript
复制
<?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();
?>
票数 4
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10938907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档