How to use SOCKS proxy

November 21, 2006

Now-a-days SOCKS proxies are getting popularity. Following PHP script can be used to browse a page using a SOCKS 5 proxy:

$url = http://www.whatismyip.com/“;
$cUrl = curl_init();
curl_setopt($cUrl, CURLOPT_URL, $url);
curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
$proxy = ‘proxy_name_or_ip:proxy_port’;
curl_setopt($cUrl, CURLOPT_PROXY, $proxy);
//Set proxy type to SOCKS 5; default is HTTP
curl_setopt($cUrl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($cUrl, CURLOPT_PROXYUSERPWD, ‘username:pass’);
$PageContent = curl_exec($cUrl);
// echo curl_error ($cUrl) . “<br>\n”;
curl_close($cUrl);
echo $PageContent;

This scripts uses cURL library. It doesn’t support SOCKS 4 proxies. If the proxy doesn’t require authentication then comment out the following line:

curl_setopt($cUrl, CURLOPT_PROXYUSERPWD, ‘username:pass’);