CURL connect to proxy server

Hi Guys!

I’m trying to connect to a proxy server using PHP CURL method. The proxy IP is a hidemyass.com. I get a blank page when I try using the following script, am I doing something wrong?


$url = "http://www.mydomain.com";
$ch = curl_init($url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_COOKIE, $cookies);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "field1=value1&field2=value2");
		curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
		curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
		curl_setopt($ch, CURLOPT_PROXY, "109.73.65.130");
		curl_setopt($ch, CURLOPT_HTTPHEADER,
			array(
			"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
			"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3",
			"Accept-Language: en-US,en;q=0.8",
			"Cache-Control: max-age=0",
			"Connection: keep-alive",
			"Content-Type: application/x-www-form-urlencoded",
			"User-Agent:".$_SERVER['HTTP_USER_AGENT'],
			"Cookie: ".implode(" ",$new_cookies)));
			
		$response = curl_exec($ch);
		print_r($response); exit;

The first thing to do is add some error checking so you can report any errors cURL may report. After doing that and testing your script, I saw it was timing out. Since proxies can be slow, I increased the timeout with no luck. So I checked the proxylist on the site you specified and I didn’t see that proxy IP listed… so it seems you may have a bad proxy.

You also didn’t specify the port for the IP. Many, if not most proxies use ports other than 80. You also have to pay attention to whether it’s an HTTP proxy or a SOCKS proxy. If it’s HTTP, you don’t have to worry about setting the cURL option as HTTP is the default.

I also removed some of the unneeded options.

Here is my working (as of this post) example:


<?php

$url = "http://www.sitepoint.com/forums/showthread.php?928518-CURL-connect-to-proxy-server";

// Testing proxy speed
$loadtime = time();

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_PROXY, "8.21.6.225:80"); // Don't forget the port of the proxy... they're not all 80

$response = curl_exec($ch);

// Testing proxy speed
echo "cURL time: " . (time() - $loadtime) . "s<br />\
";

if ($response === false) {
    echo "cURL Error: ", curl_error($ch);
} else {
    echo $response;
}

?>