Wednesday, 13 June 2012

Javascript in PHP on Windows with php_spidermonkey

Update 19 June 2014 - the latest builds of php_spidermonkey for Windows are contained in this zip:  I reported a few days ago that the PHP 5.5 TS build did not work - as of 19 June 2014 it works.  PHP 5.5 includes 64-bit versions in the x64 folder of the zip file.

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 J4P5 ( and PHPJS (; 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:
  2. Get and build Spidermonkey 1.8.5 from here:
    1. Get the source code - near the top of the page is a link to it, or you can get it from here:
    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: 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
    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.


  1. Hi - I was all excited by this post until I tried to download the dll. Then I discovered that the link to the compiled dll is broken. I'm afraid the steps you describe for the build are way outside my level of competence: would it be possible to make the dll available somewhere else?
    Many thanks

    1. Link seemed ok when I tried it - it might have been a temporary glitch on Skydrive - the link tries to open in a new window if it's any help in solving what went wrong.

  2. Hi, sorry the link didn't work, I'll have a look asap. All my downloads are on the same page, so if you go to the unofficial sqlsrv page, click the download on that page and it will take you to the download for php_spidermonkey.

  3. Sorry, I'll try from home, I just realised that for some reason I was blocked by our proxy service at work

    1. Same here, I should have thought of that. I waste half my time at work browsing the web from my phone to stop them blocking access and intercepting ssl connections.

  4. Yup, that was the issue. I didn't recognise it at first because (presumably something to do with the tinyURL) it didn't show up our normal "you have been blocked" page but just a DNS error. Anyway, now I'll have some fun trying to play with it!

  5. Hi!

    How is the dll instalation in wamp?

    Thank You! ;)

    1. Put the DLL in your php extensions folder and add it to php.ini.
      For example if you were using php 5.4 you would need php_spidermonkey_54_ts.dll

  6. Thanks for the unofficial driver for sqlsrv. Microsft neegd to load the correct dll for ease of setting up SQL for PHP.
    C:\Users\BILL>php -v
    PHP Warning: PHP Startup: Unable to load dynamic library 'ext\php_sqlsrv_56_ts.

    dll' - %1 is not a valid Win32 application.
    in Unknown on line 0
    PHP Warning: PHP Startup: Unable to load dynamic library 'ext\php_pdo_sqlsrv_56
    _ts.dll' - %1 is not a valid Win32 application.
    in Unknown on line 0
    PHP 5.6.19 (cli) (built: Mar 2 2016 20:09:42)
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies



Note: only a member of this blog may post a comment.