Jump to content

PHP Curl Proxy Strange results


Robbrad

Recommended Posts

Im getting a strange error im using the following code to cURL a webpage with proxy settings - it seams when I pass the proxy stright in I get a cannot connect message ( guess that means the proxy is being used ) which im fine with because its using the proxy(intended functionality)

 

However if i pass it in as a $var it bypasses this and curls the page directly

 

see below

 

Direct Entry of proxy address

 

function getPageData($url) {

if(function_exists('curl_init')) {
	$ch = curl_init(); // initialize curl with given url
	if((ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off')) {
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
		curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 	
	}

$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
$header[] = "Cache-Control: max-age=0"; 
$header[] = "Connection: keep-alive"; 
$header[] = "Keep-Alive: 300"; 
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
  	$header[] = "Accept-Language: en-us,en;q=0.5"; 
$header[] = "Pragma: "; // browsers keep this blank. 

  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_HEADER, $header);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_PROXY, '88.191.51.118:3128' );//Direct proxy entry of address
  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // max. seconds to execute   
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');  
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)'); 
  curl_setopt($ch, CURLOPT_FAILONERROR, TRUE); // stop when it encounters an error

 $result['EXE'] = curl_exec($ch);
    $result['INF'] = curl_getinfo($ch);
    $result['ERR'] = curl_error($ch);

    curl_close($ch);
    return $result['EXE'];

}

}

echo getpagedata("http://www.ioerror.us/ip/");

?>

 

Proxy entry via a $var

$proxy_server = '88.191.51.118:3128';

function getPageData($url) {

if(function_exists('curl_init')) {
	$ch = curl_init(); // initialize curl with given url
	if((ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off')) {
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
		curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 	
	}

$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
$header[] = "Cache-Control: max-age=0"; 
$header[] = "Connection: keep-alive"; 
$header[] = "Keep-Alive: 300"; 
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
  	$header[] = "Accept-Language: en-us,en;q=0.5"; 
$header[] = "Pragma: "; // browsers keep this blank. 

  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_HEADER, $header);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_PROXY, $proxy_server ); //entry via a $var
  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // max. seconds to execute   
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');  
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)'); 
  curl_setopt($ch, CURLOPT_FAILONERROR, TRUE); // stop when it encounters an error

 $result['EXE'] = curl_exec($ch);
    $result['INF'] = curl_getinfo($ch);
    $result['ERR'] = curl_error($ch);

    curl_close($ch);
    return $result['EXE'];

}
}
echo getpagedata("http://www.ioerror.us/ip/");

 

Thanks

Rob

Link to comment
https://forums.phpfreaks.com/topic/177776-php-curl-proxy-strange-results/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.