Javascript in PHP on Windows with php_spidermonkey

I recently had a problem to solve: run some Javascript from inside PHP on a web server.

I needed to evaluate a web browser's PAC (proxy auto config) file exactly the way a browser would, by executing the Javascript function FindProxyForURL() (see here for a good guide). The browser uses its Javascript engine, so to simulate it, I wanted to run Javascript inside PHP (an example is here in another post). 

On the web there were some solutions for Javascript engines, written in PHP, such as:

The problem is that these solutions, however impressive, are not the real thing. There will be nagging worries about reliability and speed.

So then I looked at Mozilla and wondered if their Spidermonkey engine could be used. I didn't have to look much further to see that there was already a fantastic PHP PECL package written by Christophe Robin that wraps the Mozilla Spidermonkey engine. Theoretically a perfect solution, giving speed, efficiency and reliablity - the only hurdle was to get it built on Windows - it wasn't a piece of cake, but worth the effort, and nothing compared to the effort of the programmers who wrote the code.

I am posting the binaries here so that others can get up and running instantly. The instructions on how to build these follow:
Steps to build - you don't need to build anything, the dll's for Windows are above - these are to help me remember what to do next time around, and maybe will be useful to someone:
  1. For builds to do with PHP, I use this VC9 SDK from Microsoft: http://www.microsoft.com/en-us/download/details.aspx?id=3138
  2. Get and build Spidermonkey 1.8.5 from here: https://developer.mozilla.org/En/SpiderMonkey/Build_Documentation.
    1. Get the source code - near the top of the page is a link to it, or you can get it from here: http://ftp.mozilla.org/pub/mozilla.org/js/.
    2. Get the build tools for Windows - near the top of the page is a list of platforms - click on Windows, then about half way down the page is a link to the Mozilla build tools entitled "Download the current MozillaBuild package".
    3. Once the build tools are installed, run "start-msvc9.bat", as documented on the Windows page. It opens a Mozilla command window (a unix bash simulation).
    4. Go back to the Build Documentation and follow the instructions for "Easy Build".
    I had no problems building, everything worked perfectly and I ended up with a static JS library, a DLL and js.exe. I chose to use the static library to build php_spidermonkey so that it could be installed as a stand-alone extension in PHP, with no other dependencies.
  3. Building php_spidermonkey is a bit more tricky. Get the original source code here: http://pecl.php.net/package/spidermonkey/1.0.0. It does not build on Windows VC9 at all without adjustment, but the code is sound - it's just minor technicalities and looks worse than it is.You need a working PHP build environment to build the extension. This won't make any sense unless you know how to build PHP extensions - these instructions work with my modified source code...:

    1. Create a spidermonkey folder in php extensions, and copy this (modified) source code into it: php_spidermonkey source (submitted to https://github.com/christopherobin/php-spidermonkey)
    2. Get the recently built javascript static library "js_static.lib" located in "js\src\dist\lib", copy it to PHP build environment folder "php54dev\vc9\x86\deps\include\lib", and rename it "js32.lib"
    3. Create sub-folder "php54dev\vc9\x86\deps\include\js", and copy all of the .h files from the spidermonkey api folder "js\src" into it.
    4. Add "--with-spidermonkey=shared" to your PHP configure command.
    5. Build PHP.