Showing posts with label FindProxyForURL. Show all posts
Showing posts with label FindProxyForURL. Show all posts

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