If you are new to using PHP and cURL please refer to the PHP cURL tutorial. It covers why you should using cURL (as opposed to file_get_contents) and goes over how to make cURL requests with a custom class.
This tutorial will cover how to use different types of proxies – SOCKS4, SOCKS5 and HTTP with cURL.
Basic cURL Request
We’ll start by creating a simple cURL script that requests the web page checkip.dyndns.org and displays the result on the screen. The website just displays our current IP address, this is useful when working with proxies. Below is our very basic cURL request.
$url = 'http://checkip.dyndns.org/'; $c = curl_init($url); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($c); echo '<pre>'; var_dump($result); echo '</pre>'; curl_close($c);
As we can expect, the output will be your IP address.
Using Proxies with cURL
To use proxies with cURL we need to set 2 additional cURL options: CURLOPT_PROXYTYPE and CURLOPT_PROXY. CURLOPT_PROXYTYPE should be set to either CURLPROXY_SOCKS5 or CURLPROXY_SOCKS4, depending on the type of proxy you are using. CURLOPT_PROXY should be set to the IP address and port of the proxy you are using.
If you are using an HTTP proxy then you only need to set CURLOPT_PROXY to your proxy. CURLOPT_PROXYTYPE automatically defaults to CURLPROXY_HTTP.
As you can expect, the following script displays the IP address of the proxy.
$url = 'http://checkip.dyndns.org/'; $c = curl_init($url); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); curl_setopt($c, CURLOPT_PROXY, '126.96.36.199:27977'); $result = curl_exec($c); echo '<pre>'; var_dump($result); echo '</pre>'; curl_close($c);
Note: You’ll need to use your own proxy. The one supplied is made up.