Robbrad Posted October 15, 2009 Share Posted October 15, 2009 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 More sharing options...
Robbrad Posted October 15, 2009 Author Share Posted October 15, 2009 changed it round to do this now $proxy_server = '124.6.186.38'; $proxy_port = '3128'; [...] curl_setopt($ch, CURLOPT_PROXY, $proxy_server.':'.$proxy_port); New Error Proxy CONNECT aborted Link to comment https://forums.phpfreaks.com/topic/177776-php-curl-proxy-strange-results/#findComment-937528 Share on other sites More sharing options...
Robbrad Posted October 15, 2009 Author Share Posted October 15, 2009 Im so stupid - the second function "doesn't know what $proxy_server is" because im not passing it as a var oh well lesson learned Link to comment https://forums.phpfreaks.com/topic/177776-php-curl-proxy-strange-results/#findComment-937597 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.