httpwatch使用_使用PHP自动化HTTPWatch

httpwatch使用

httpwatch使用

HTTPWatch is a nice tool to inspect HTTP traffic in easy and convenient way and it works in both IE and FF now. Drawback - windows-only and paid. But the free version is good enough for many tasks.

HTTPWatch是一种轻松便捷地检查HTTP流量的好工具,并且现在可以在IE和FF中使用。 缺点-仅Windows和付费。 但是免费版本足以应付许多任务。

HTTPWatch can be automated and scripted which is pretty cool for a number of monitoring-like tasks. Their site and help section lists C# and Ruby+Watir examples. So I was curious - what about PHP (and no Watir).

HTTPWatch可以自动化和编写脚本,对于许多类似于监视的任务来说非常酷。 他们的站点和帮助部分列出了C#和Ruby + Watir示例。 所以我很好奇-那么PHP(而不是Watir)呢?

In general with PHP you can open/close/navigate IE using COM (whatever that is) which is nice, but you can't do that with Firefox as it doesn't expose a COM interface. But HTTPWatch fills the gap. K, let's see an example.

通常,使用PHP可以使用COM (无论如何)打开/关闭/导航IE,这很好,但是由于Firefox不公开COM接口,因此您不能这样做。 但是HTTPWatch填补了空白。 K,让我们看一个例子。

先决条件 (Prerequisites)

OS: Windows Install: IE, Firefox, HTTPWatch, php (command-line is fine, no need for Apache, MySQL, etc)

操作系统:Windows Install:IE,Firefox,HTTPWatch, php (命令行很好,不需要Apache,MySQL等)

入门 (Getting started)

Create a file, say C:\http.php, open command prompt and go:

创建一个文件,例如C:\http.php ,打开命令提示符并转到:

cd \
C:\>php http.php

Now all that's left is to put something worth executing in http.php 🙂

现在剩下的就是在http.php中放入值得执行的内容了。

Instantiating HTTPWatch

实例化HTTPWatch

$controller = new COM("HttpWatch.Controller");
if(!method_exists($controller, 'IE')) {
  throw new Exception('failed to enable HTTPWatch');
}

Opening a new Firefox window:

打开一个新的Firefox窗口:

$plugin = $controller->Firefox->New();

BTW, it's the same for IE:

顺便说一句,对于IE来说是一样的:

$plugin = $controller->IE->New();

Disabling any filters (filters defined in HTTPWatch that is)

禁用任何过滤器(即在HTTPWatch中定义的过滤器)

$plugin->Log->EnableFilter(false);

Clear HTTPWatch's log (the list of requests), clear the browser cache and start recording traffic:

清除HTTPWatch的日志(请求列表),清除浏览器缓存并开始记录流量:

// clear log and cache
$plugin->Clear();
$plugin->ClearCache();
 
// start
$plugin->Record();

Navigate to a URL and wait for it to complete - that means wait a bit after onload even

导航到一个URL并等待其完成-这意味着在onload之后还要稍等片刻

// browse
$plugin->GotoUrl('http://google.com');
$controller->Wait($plugin, -1);

Stop monitoring traffic and quit the browser:

停止监视流量并退出浏览器:

$plugin->Stop();
$plugin->CloseBrowser();

This is nice, we opened the browser, visited a URL and closed. Now we can even get some meaningful data out of the whole experience.

很好,我们打开了浏览器,访问了URL,然后关闭了。 现在,我们甚至可以从整个体验中获取一些有意义的数据。

$plugin->Log->Entries is an object that has a list of all requests. It also has a property Summary. So we can see how many bytes we sent and how many received as a result of this visit to google.com

$plugin->Log->Entries是一个对象,其中包含所有请求的列表。 它还具有属性Summary 。 这样一来,我们就可以查看由于访问google.com而发送的字节数和收到的字节数

$sum = $plugin->Log->Entries->Summary;
echo "in: {$sum->BytesReceived}, out: {$sum->BytesSent}";

Note: oh, you need to get your data before closing the browser, otherwise the Log object gets destroyed it seems

注意:哦,您需要在关闭浏览器之前获取数据,否则Log对象似乎被破坏了。

So the result:

结果是:

C:\>php http.php
in: 89185, out: 7102

是的 (Yeah!)

This may look like nothing, but is pretty impressive in an of itself. At least I know I was happy the first time it worked. Because, you see, any monitoring that doesn't use a real browser is kinda smelly, isn't that right? Plus this is awesome for performance tests, research and experiments. You can create page A and page B and go out for a walk. Meanwhile your script can load the pages 200 times in the two browsers (at least, because you can have FF+IE[678]), with empty and full cache... and you come back for the results! Tired of all the walking, not of hitting REFRESH.

这看起来似乎什么都没有,但其本身令人印象深刻。 至少我知道我第一次感到高兴。 因为,您看到,任何不使用真实浏览器的监控都有些臭,不是吗? 另外,这对于性能测试,研究和实验来说非常棒。 您可以创建页面A和页面B并出去散步。 同时,您的脚本可以在两个浏览器中加载页面200次(至少是因为您可以拥有FF + IE [678]),并且具有空的和完整的缓存...然后您返回获取结果! 厌倦了所有的步行,而不是刷新。

Below you can see (HD!) video of a script that opens IE and FF, loads Google and then gives you the bytes in/out in the two browsers. This example uses a PHP class I created and will talk about later, but you can still see the idea.

在下面,您可以看到(HD!)脚本的视频,该脚本打开IE和FF,加载Google,然后在两个浏览器中输入/输出字节。 这个例子使用了我创建的一个PHP类,稍后将讨论,但是您仍然可以看到这个想法。

更好的IE体验 (A better experience in IE)

One thing I don't like is that HTTPWatch won't let you control the browser very well. Two features I'm looking for: being able to see HTTPWatch's log while running (for testing) and then being able to completely hide the window (for "production"). Luckily IE let's you do that and HTTPWatch let's you "attach" an already running IE instance.

我不喜欢的一件事是HTTPWatch无法让您很好地控制浏览器。 我正在寻找两个功能:能够在运行时查看HTTPWatch的日志(用于测试),然后能够完全隐藏窗口(用于“生产”)。 幸运的是,IE让您执行此操作,而HTTPWatch让您“附加”已在运行的IE实例。

So. We open IE with its own COM interface:

所以。 我们使用自己的COM接口打开IE:

$browser = new COM("InternetExplorer.Application");
if(!method_exists($browser, 'Navigate')) {
  throw new Exception('didn\'t create IE obj');
}
$browser->Visible = true;

As you can see - not very different. But there's Visibile which can be false if you so like. This way you can still work on something while tests are running in the background without windows popping up all the time.

如您所见-差别不大。 但是,如果您愿意, Visibile可能是错误的。 这样,您仍可以在后台运行测试时进行某些操作,而不会一直弹出窗口。

Also if you open HTTPWatch manually and close the browser, then the next time (in your scripted runs) HTTPWatch will stay open and you can check what's up.

另外,如果您手动打开HTTPWatch并关闭浏览器,那么下次(在脚本运行中)HTTPWatch将保持打开状态,您可以查看最新情况。

So, connecting HTTPWatch with the IE instance means instantiating HTTPWatch as before and passing the IE object to Attach() method (was New() before).

因此,将HTTPWatch与IE实例连接意味着像以前一样实例化HTTPWatch,并将IE对象传递给Attach()方法(之前是New() )。

// watch this!
$controller = new COM("HttpWatch.Controller");
if(!method_exists($controller, 'IE')) {
  throw new Exception('failed to enable HTTPWatch');
}
 
// enable plugin
$plugin = $controller->IE->Attach($browser);

The rest is all the same.

其余的都一样。

还有更多 (There's more)

The most interesting part is getting data back from HTTPWatch. Dunno about you, but I love just dumping whatever structure I have with print_r() or var_dump() and then deciding what I want from it and how to to go about getting it.

最有趣的部分是从HTTPWatch取回数据。 Dunno关于您,但是我喜欢只使用print_r()或var_dump()转储我拥有的任何结构,然后决定要从中获取什么以及如何进行获取。

That doesn't happen here because these COM objects are Variants and you can't just dump'em. You have to read the API docs. That sucks. So I did a hack (next post) and also read the APIs ("Stoyan: reading the APIs so you don't have to!") to enable just dumping the httpwatch's log.

这里不会发生这种情况,因为这些COM对象是Variants,并且您不能只是dump'em。 您必须阅读API文档。 糟透了因此,我做了一个hack(下一篇文章),并且还阅读了API(“ Stoyan:阅读API,因此您不必这样做!”)以仅转储httpwatch的日志。

Meawhile...

同时...

哈尔 (HAR)

HTTPWatch can write you a HAR file with the log. Not everything is in there, but it's still a lot and it's easy. HAR is JSON so you json_decode() it and voila - a log!

HTTPWatch可以使用日志向您写入一个HAR文件。 并非所有内容都在其中,但仍然很多,而且很容易。 HAR是JSON,因此您可以json_decode()并查看-日志!

$filename = tempnam('/tmp', 'watchmenowimgoindown');
$plugin->Log->ExportHAR($filename);
$json = file_get_contents($filename);
 
print_r(json_decode($json));

If you're curious as to what that prints - here it is.

如果您对打印内容感到好奇-就在这里

Want to see a HAR (from another run)? Here it is.

是否想查看HAR(从另一次运行)? 在这里

So here you go - much data can be extracted and dumped for inspection from the HAR output. For the full httpwatch data, there's the API.

因此,您可以从HAR输出中提取和转储大量数据以进行检查。 有关完整的httpwatch数据,请参阅API

(to be continued...)

(未完待续...)

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/automating-httpwatch-with-php/

httpwatch使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值