V
V
Vitaliy Semyanchuk2014-05-15 13:59:47
PHP
Vitaliy Semyanchuk, 2014-05-15 13:59:47

Why is recursion not working in php?

Hello.
Guys, help, it seems to be an easy task, but I can not solve it myself.
There is a code that pulls pages through url

function getData($url,$post=''){
  $dir = dirname(__FILE__).'/cookie.txt';
  $ch=curl_init();   
  $proxy = lastIp();
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 35);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 35); 
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  //Переходим по редиректам
  curl_setopt ($ch , CURLOPT_RETURNTRANSFER , 1 );	
  curl_setopt($ch, CURLOPT_PROXY, $proxy);
  //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  curl_setopt($ch, CURLOPT_FAILONERROR, true);
  //curl_setopt($ch, CURLOPT_POST, true);
  //curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  curl_setopt($ch, CURLOPT_REFERER, "https://www.google.ru/");
  curl_setopt($ch, CURLOPT_COOKIEJAR, $dir);  // Записываем cookies в файл, чтобы потом можно было их считать
  curl_setopt($ch, CURLOPT_COOKIEFILE, $dir); // Теперь читаем cookies с файла;
  curl_setopt($ch, CURLOPT_USERAGENT, get_random_user_agent()); 
  $res=curl_exec($ch);		
  curl_close($ch);

  if(strlen($res)>0){
    updateIp($proxy,'success');
    return $res;
  }else{
    updateIp($proxy,'error');
    logs('Bad proxy '.$proxy.' on url '.$url);
    unset($res);
    sleep(1);
    getData($url);
    logs('recursia not work');
    return false;
  }
  
}

I call it getData($url,$post='') and in theory it should be repeated until something is cut. But for some reason it is not overloaded, when I write getData($link) in the code, it continues to work and returns me an empty value.
Who has any ideas?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Lesnykh, 2014-05-15
@Aliance

Because in the else condition branch, the recursive function call does not return anywhere.

M
MaximAL, 2014-05-15
@MaximAL

One of the necessary requirements of recursion is the mandatory possibility of a non-recursive exit.
This means that the return from the function for some kind of check somewhere must go before the recursive call.
There is no such case in your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question