Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP简单HTML DOM解析器的故障保护_Php_Dom_Domparser - Fatal编程技术网

PHP简单HTML DOM解析器的故障保护

PHP简单HTML DOM解析器的故障保护,php,dom,domparser,Php,Dom,Domparser,使用PHP Simple HTMLDOM Parser(),我最近遇到了一个情况,我通常获取的外部网页没有响应(它们的服务器关闭)。因此,我自己的网站不会加载(而是在长时间等待后显示错误) 在尝试获取失败时,向该解析器添加故障保护的最佳方法是什么 我曾尝试使用以下方法,但没有成功 include('./inc/simple_html_dom.php'); $html = file_get_html('http://client0.example.com/dcnum.php?count=1'

使用PHP Simple HTMLDOM Parser(),我最近遇到了一个情况,我通常获取的外部网页没有响应(它们的服务器关闭)。因此,我自己的网站不会加载(而是在长时间等待后显示错误)

在尝试获取失败时,向该解析器添加故障保护的最佳方法是什么

我曾尝试使用以下方法,但没有成功

include('./inc/simple_html_dom.php');  

$html = file_get_html('http://client0.example.com/dcnum.php?count=1');
$str = $html->find('body',0);
$num = $str->innertext;

if(!$html)
{
 error('No response.')
}

$html->clear(); 
unset($html);

编辑:我还没有时间尝试这一点,但也许我可以将我的'if'语句直接放在第一行之后(在$html->find('body',0)部分之前)。

如果我知道你想在他们脱机时阻止他们脱机

如果您使用的是PHP的curl绑定,则可以使用curl_getinfo检查错误代码,如下所示:

$handle = curl_init($url);
curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);

/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    /* Handle 404 here. */
}

curl_close($handle);

/* Handle $response here. */

您还可以检查其他错误代码,如500503等。

我花了几个小时才弄明白这一点,令人惊讶的是,关于如何使用简单的html\U dom处理错误的线索很少

基本上,你所要做的就是摆脱
文件\u get\u html
->加载文件
,或者你用来加载内容的任何简单的\u html\u dom特定方法,改为使用curl,并将其传递给
str\u get\u html

我使用了另一个答案的代码,下面是如何使用它:

function get_with_curl_or_404($url){
    $handle = curl_init($url);
    curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);

    $response = curl_exec($handle);

    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

    curl_close($handle);

    if($httpCode == 404 || !$response) { // arbitrary choice to return 404 when anything went wront
        return 404;
    } else {
        return $response;
    }
}

$html = str_get_html(get_with_curl_or_404("http://your-
url.com/index.html"));
if ($html == 404) {
     // Do whatever you want
} else {
     // If not 404, you can use it as usually, ->find(), etc
}
如果它在大型网站上更稳定

如果这是你想要的行为,请尝试一下,告诉我我没有让你开心