I'm going insane trying to figure out what is the problem but I can't find it.
$proxies = loadProxies(5);
function getData($proxylist)
{
$rand_proxy = rand(0,count($proxylist)-1);
$url = 'http://www.stackoverflow.com'; //just for example
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.4 (KHTML, like Gecko) Chrome/4.0.233.0 Safari/532.4";
$referer = "http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_PROXY, $proxylist[$rand_proxy]);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
getData($proxies);
It should fetch a random proxy IP from the array and then use it in cURL request. All data I'm getting is a blank page. In some cases, I get infinite page load with no results whatsoever. What is causing this and how do I fix this? Thanks.
maybe the problem is with your loadProxies(5) return? cus this code works fine right here right now:
<?php
$proxies = array('86.188.142.244:8080'); // random public http proxy
function getData($proxylist)
{
$rand_proxy = rand(0,count($proxylist)-1);
$url = 'http://www.stackoverflow.com'; //just for example
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.4 (KHTML, like Gecko) Chrome/4.0.233.0 Safari/532.4";
$referer = "http://www.google.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_PROXY, $proxylist[$rand_proxy]);
$data = curl_exec($ch);
if($data!==true){$ex=new RuntimeException('curl_exec error. errno: '.curl_errno($ch).' error: '.curl_error($ch));@curl_close($ch);throw $ex;}
curl_close($ch);
//echo $data;
}
getData($proxies);
also, $data just returns the return code for curl, not the data, because, by default, curl outputs the response body to stdout. if you set CURLOPT_RETURNTRANSFER , it will return the body. (to redirect it to something else, use CURLOPT_FILE )