2014-07-10 33 views
3

我的脚本尝试从文件中逐行读取每个站点,但是当尝试解析不存在的站点时,脚本停止并给我出现以下错误:当网站不存在时,出现简单的HTML Dom错误

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 76 

Warning: file_get_contents(http://www.thissitedoesntexist.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\simple_html_dom.php on line 76 

Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\test2.php on line 11 

我该如何解决?我怎样才能让它运行,即使一个网站不存在...从我的文件(这意味着读取另一个网站,然后阅读它)阅读一个新的行也当scrip停止时,像404,403接收错误等

+0

记住你的代码的外观吗? – Steven

+0

添加于我的帖子 – user3680499

+0

这是实际的代码?我看不到'file_get_contents()'?... – War10ck

回答

1

根据文档,file_get_contents()返回FALSE失败。 因此,在尝试解析返回的内容之前,请检查它返回的内容以确保网站存在。如果它不存在,遍历到文件中的下一行,并保持持续的过程:

if($file = file_get_contents("http://www.thissitedoesntexist.com")) { 
    // Parse file here 
    // Then continue reading the file by 
    // starting at the next line. 
    continue; 
} 

参考:

file_get_contents()

1

我会做到这一点,以检查错误代码---

foreach ($sites as $site) { 

    $ch = curl_init('http://'.$site); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Execute 
    curl_exec($ch); 

    // Check if any error occurred 
    $info = curl_getinfo($ch); 

    if($info['http_code'] != 200) { 
     continue; 
    } 
    //rest of loop, here -- 

} 

你甚至可以做一些不同的取决于你得到一个case-swi的错误代码tch -

相关问题