找不到字符串时重试功能

时间:2014-04-03 20:59:29

标签: php curl

我有一个使用curl代理列表来抓取数据的函数。每次调用函数时,它都会选择一个随机代理。但是,有时代理可能会失败或超时。

当连接失败/超时时,我想重复该功能最多3次,直到返回数据。

我想测试连接是否错误的方法是检查输出中是否存在字符串,如下所示:

$check = stripos($page,'string_to_check');
if($check > 0){ 
    return $page; //String found. Return scraped data. 
}
else { 
    //String not found. Loop the script 
}

如果字符串不存在,如何重复整个功能代码?

1 个答案:

答案 0 :(得分:0)

$max_tries = 3;
$success = false;

//try 3 times
for( $i = 0; $i < $max_tries; $i++ ) {
  $page = your_scrape_function();
  $check = stripos($page,'string_to_check');
  if($check > 0){
    $success = true;
    break; //String found. Break loop.
  }
}

// double check that the string was actually found and you didn't just exceed $max_tries
if( ! $success ) {
  die('Error: String not found or scrape unsuccessful.');
}