找不到字符串时重试功能


Retry function when string not found

我有一个函数,它使用带有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 
}

如果字符串不存在,我将如何让整个函数代码重复?

$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.');
}