使用PHP调用Httpwatch.controller 来分析httpwatch的log文件
httpwatch 官方帮助文档中提供了用 JavaScript、Ruby、C# 分析 log 文件的示例,却没有 PHP 版本。作者起初用 JavaScript 尝试,在解析 Request.Stream 和 Response.Stream 时卡住了——Stream 以字节数组形式出现,难以转成字符串,尤其当 Response 经过 gzip 压缩时更不好处理,于是转而用 PHP 通过 COM 调用 HttpWatch.Controller 来实现。
示例代码的思路是:用 COM 创建 HttpWatch.Controller 对象并 OpenLog 打开 .hwl 文件,遍历 Entries 中的每条记录(item 是方法而非数组),输出 ClientIp、ServerIp 等信息,并分别解析 Request 和 Response 的 Stream。其中 decode 函数逐字节用 chr 拼出字符串,注意求长度要用 count 而非 strlen。getResponse 先按 \r\n\r\n 切分出 header 和 content,再根据 Transfer-Encoding 为 chunked 时用 getChunkedContent 按十六进制块大小还原内容,Content-Encoding 为 gzip 时做解压——不过作者注明此处 gzuncompress 目前仍有问题,原因未知。整个脚本以 php httpwatch.log.php a.hwl 的方式运行。