Print-Clearly

Browse Site

Developing with CURL behind a Proxy

I carry out site development in a local development environment (LAMP, WAMP, MAMP, XAMPP, etc.) on a network that sits behind a proxy server. The production environments that those sites end up working from don’t suffer the same proxy-based hardship but it does mean that – without intervention – certain functions don’t work on my localhost when I take a project out of the repository. I get around that by using the following code and script to switch between having, and not having, a proxy server to contend with.
By having a single include file in your root directory (and making sure that your localhost’s root directory is secure enough) you can call it into your script whenever it recognises that your site is running on a local apache server.
As with anything, there is more than one way to set up the options of a proxy server when using CURL. This solution works with both.

Example 1 | Setting Options In An Array

 $curl_handle = curl_init(‘www.blahblahblah.com’);
 $curl_options = array(
   CURLOPT_RETURNTRANSFER => true,
 ...
 );
 if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false)
 {
   require_once(‘/proxy_details.inc.php');
   $curl_options = add_proxy($curl_options);
 }
 curl_setopt_array($curl_handle, $curl_options);

Example 2 | Setting Options Using Setopt

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, ‘www.blahblahblah.com’);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, …);
 // additional proxy info if sending from my localhost
 if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false)
 {
   require_once(‘/proxy_details.inc.php');
   $ch = proxy_setopt($ch);
 }
 curl_exec($ch);

I would suggest that ‘best use’ (which in this case means ‘desirable but not always practical’) would be to delete any reference to your proxy server before your site goes into a production state. However if you want to keep your production and development copies identical or in case you simply miss it, by using …

if (strpos($_SERVER['HTTP_HOST'], 'localhost') !== false)

… you can ensure that this file won’t be included when on live and your curl queries can go direct to the source.

The contents of my include are: proxy_details.inc.php

define("NETWORK_PWD", base64_decode(ENCODED_PWD));
function add_proxy($curl_resource)
{
 if (is_array($curl_resource))
 {
   $proxy_settings = array(
   CURLOPT_PROXYPORT => PROXY_PORT,
   CURLOPT_PROXY => PROXY_SERVER,
   CURLOPT_PROXYAUTH => CURLAUTH_NTLM,
   CURLOPT_PROXYUSERPWD => sprintf('%s:%s', NETWORK_USER, NETWORK_PWD));
   $curl_resource = $curl_resource + $proxy_settings;
 }
 else
 {
   curl_setopt($curl_resource, CURLOPT_PROXYPORT, PROXY_PORT);
   curl_setopt($curl_resource, CURLOPT_PROXY, PROXY_SERVER);
   curl_setopt($curl_resource, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
   curl_setopt($curl_resource, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', NETWORK_USER, NETWORK_PWD));
 }
return $curl_resource;
}

I keep my network password base_64 encoded in this script. This isn’t a perfect solution but I’m happy that my password is secure enough should a non-techie come across it. By keeping this include file in the root of my localhost I should prevent ever putting the file into a repository or onto a live site.

What this solution also provides is time. Our network demands that I reset my password every month and with this solution I only need to update one location each time to roll out the change to every project that I have checked out. This also means that my scheduled tasks, php command scripts and any other sites that access an external resource work immediately without the need to get into the code and change it every time I need it.

[In the interests of balance and recognising the limitations to any given solution, this script works fine unless you’re in the unusual position of working on a site with ‘localhost’ as a part of the domain name!]

This entry was posted in Geeky Stuff, php.

Comments are closed.