Wednesday, 13 June 2012

FindProxyForURL - Parse a PAC (proxy auto config) file in PHP

Proxy servers are used in most companies as a gateway to the internet, that do other things like cache results and check web requests and responses.  A PAC file is a Javascript file evaluated by web browsers to determine which proxy server to use for certain URL's.  For example, some URL's can go straight to the internet, or a local server, and others must be sent to one of the proxy servers.

One day I needed to run this javascript from php on the web server, to find the proxy in the same way; then php could also follow the same proxy rules as desktop computers in my office.

Here's a working example of using the php_spidermonkey extension to run a proxy auto config script (javascript) in PHP.

Javascript Return Statement

After 20+ years of programming I learnt something new today, while messing about with the php_spidermonkey extension.

If you place a carriage return after a 'return' statement in Javascript, and put some code on the line underneath, the function returns 'undefined', as if the statement was simply: return;

Here's an example of a function that returns 'undefined':
function whatelse()
{
    var ok = false;
    return // function ends here
        ok ? 'OK (true)' : 'OK (false)'; // this line is never executed!
}

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: all_php_builds.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). 

Unofficial Microsoft SQL Server Driver for PHP (sqlsrv)

Here are unofficial modified builds of Microsoft's sqlsrv and pdo_sqlsrv drivers for PHP, version 3.0, for:

  • PHP 5.6 for Windows Vista/2008 and above (32 and 64 bit) 
  • PHP 5.5 for Windows Vista/2008 and above (32 and 64 bit) 
  • PHP 5.3 and 5.4 for Windows XP/2003 and above (32 bit)
    Microsoft's version 3.0 driver does not run on XP/2003 because it has a hard-coded dependency on the SQL Server 2012 Native Client (ODBC driver), which will not install on XP.  To fix the problem I just had to allow the PHP driver to use an older version of the ODBC driver supported by XP.