V
V
Vitaliy Semyanchuk2013-11-29 14:07:05
Web development
Vitaliy Semyanchuk, 2013-11-29 14:07:05

Curl, proxy hangs

Good afternoon.
I have a problem, please suggest a solution.
There is a script that takes information through the curl library, from my site on Yukoz, well, in order not to block Yukoz my IP, I use a proxy. Here is the problem, that if the proxy address does not work, then my script spins forever and does nothing. Tried different ways to get away but not how it doesn't work.
function getData($url){
$proxy=lastIp();
if(isset($proxy)){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // Write the cookies to a file so we can read them
later curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // Now read the cookies from the file;
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.1) Gecko/2008070208');
$res=curl_exec($ch);
if(curl_error($ch))
{
/*
$error = curl_error($ch).'('.curl_errno($ch).')
';
echo $error."
";
curl_close($ch);
deleteIp($proxy);
*/
flush();
getData($url);
}
else
{
if(preg_match("
echo "Forbidden
";
deleteIp($proxy);
flush();
getData($url);
}else{
curl_close($ch);
deleteIp($proxy);
flush();
return $res;
}
}
curl_close($ch);
deleteIp($proxy);
flush();
return $res;
}else{
echo "End of proxy address
";
getProxyList();
flush();
getData($url);
}
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey, 2013-11-29
@ScorpLeX

You do not have the CURLOPT_PROXYTYPE proxy type set, so it would be better to add
CURLOPT_FAILONERROR as well.
You also need to check the lastIp and deleteIp functions.

V
Vitaliy Semyanchuk, 2013-11-29
@fix20152

Check lastIp and deleteIp checked they work, only my problem is that deleteIp does not reach, that is, if the proxy is not working, then it hangs and the function cannot delete this IP and start over again. Here is the snag.

V
Vitaliy Semyanchuk, 2013-11-29
@fix20152

Tried CURLOPT_PROXYTYPE and CURLOPT_FAILONERROR. still hangs

V
Vitaliy Semyanchuk, 2013-11-29
@fix20152

All thanks earned, here it is ready, maybe someone will need
function getData($url){
$proxy=lastIp();
if(isset($proxy)){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // Write cookies to a file so that we can read them later
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // Now read the cookies from the file;
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.1) Gecko/2008070208');
$res=curl_exec($ch);
if(curl_error($ch))
{
/*
$error = curl_error($ch).'('.curl_errno($ch).')
';
echo $error."
";
curl_close($ch);
deleteIp($proxy);
*/
$error = curl_error($ch).'('.curl_errno($ch).')
';
echo $error."
";
deleteIp($proxy);
flush();
getData($url);
}
else
{
if(preg_match("/Forbidden/i",$res)){
echo "Forbidden
";
deleteIp($proxy);
flush();
getData($url);
}else{
curl_close($ch);
deleteIp($proxy);
flush();
return $res;
}
}
curl_close($ch);
deleteIp($proxy);
flush();
return $res;
}else{
echo "End of proxy address
";
getProxyList();
flush();
getData($url);
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question