Firefox Browser Automation with mozrepl – A PHP Class

Browser automation can be an effective method where command line clients like cURL will fail. There are a number of better known browser automation tools like Selenium and AutoIt that can achieve a lot in themselves. The prevailing use for browser automation is to imitate a real-world user and should only be used where necessary, as it’s a lot more efficient to use a command line client which uses far less computer resources.

A lesser known plugin for Firefox called mozrepl is an excellent browser automation tool which gives complete control of your browser from top to bottom. Think of everything your browser does, its extensions and more…

mozrepl gets its name from the generic definition of REPL, and I assume the “moz” refers to Mozilla (Firefox). mozrepl is a firefox-plugin that allows you to telnet into your browser, whether it is local or on a remote machine. You communicate with mozrepl via telnet, giving it Javascript commands which it’ll process synchronously and return the resulting data back to you, if any.

I could go on and on about the various ways you can utilise it, instead I’ll post some real-world examples and code that can inspire you. Meanwhile, the remainder of this article will describe how to get mozrepl running, as well as using a generic PHP class that’ll communicate with mozrepl via telnet.

How to Install Mozrepl

Install the add-on
– Restart your browser if required
– Go to Tools > Mozrepl and ensure Activate on Startup is checked. Also click Start if mozrepl has not already started
– There is also an option to Allow outside connections should you wish to control the browser over a network. For now, testing on your local machine should be fine.

A basic PHP class to use mozrepl via telnet

Basic Example

After including the above, you can test it is working by using some sample Javascript.

Run this from the command line. You can run it in your browser if you like but it makes more sense to run it from the command line.

I’d recommend spending an hour reading through the mozrepl guide to get an idea of the general scope of the add-on. Keep checking back to for some real-world examples.

A Simple Generic PHP cURL Class

cURL is a most excellent library that enables you to communicate across a network with a number of protocols. PHP integrated the cURL library early in its development and nowadays its use is widespread across countless applications. To read more about it you can visit the author’s home page, Daniel Steinberg.

I use a very short generic class that can deal with the majority of my network requests, which is available to you below.

An example request and output is shown below.

Much of the magic in this generic class happens in the curl_setopt_array() function call, which sets the options described on this page. cURL pretty much covers all angles, hence the huge list of options you can pass to your request.

There is also the option of performing multiple requests should you require, which is slightly more efficient.

The nice thing about using cURL for network requests is that there is a huge amount of documentation, and the author continues to provide assistance on various forums and sites to this day.