【问题标题】:file_get_html failed to open stream: Connection refused infile_get_html 无法打开流:连接被拒绝
【发布时间】:2017-03-03 18:05:06
【问题描述】:

我得到了这个函数来从外部 URL 获取一些信息。 问题是,如果网站有 robots no-index 这个函数会崩溃,然后导致 foreach 循环崩溃。

错误信息:

警告:file_get_contents(http://webontwerp-arnhem.nl/contact):无法打开流:第 79 行 /var/www/vhosts/free-sitemap-generator.com/httpdocs/includes/cra/simple_html_dom.php 中的连接被拒绝

致命错误:未捕获的错误:调用 /var/www/vhosts/free-sitemap-generator.com/httpdocs/includes/cra/xml-functions.php:60 中布尔值的成员函数 find() 堆栈跟踪: #0 /var/www/vhosts/free-sitemap-generator.com/httpdocs/crawler.php(44): crawl_site('http://webontwe...') #1 {main} 抛出 /var/www/ vhosts/free-sitemap-generator.com/httpdocs/includes/cra/xml-functions.php 第 60 行

功能:

function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
{
    $dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
    $contents = file_get_contents($url, $use_include_path, $context, $offset);

    if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
    {
        return false;
    }

    $dom->load($contents, $lowercase, $stripRN);
    return $dom;
}

使用循环调用函数:

function crawl_site($u) {
    $urlList = array();
    global $crawled_urls, $found_urls;
    $uen = urlencode($u);
    if ((array_key_exists($uen, $crawled_urls) == 0 || $crawled_urls[$uen] < date("YmdHis", strtotime('-25 seconds', time())))) {
        $html = file_get_html($u);

        $crawled_urls[$uen] = date("YmdHis");
        foreach($html -> find("a")as $li) {
            $url = perfect_url($li -> href, $u);
            $enurl = urlencode($url);
            $str = basename($url);
            $dirn = dirname($url);
            if ($url != '' && substr($url, 0, 4) != "mail" && substr($url, 0, 3) != "tel" && substr($url, 0, 5) != "phone" && substr($url, 0, 5) != "skype" && substr($url, 0, 4) != "java" && array_key_exists($enurl, $found_urls) == 0) {
                $found_urls[$enurl] = 1;
                $pos = strpos($str[0], '#');
                $ext = strpos($url, $u);
                if ($ext != = false && $pos == = false) {
                    echo "<li><div class='url-row'>$dirn/<span class='strong'>$str</span></div></li>";

                    array_push($urlList, $url);
                }
            }
        }
    }
}

【问题讨论】:

  • 如果错误出现在 $html-&gt;find("a") ,在该行之前进行“if”检查:if($html){ .... 之后尝试执行“foreach”
  • 是的,我试过了,但是例如:如果 site.com/page_1 没有机器人元标记,但如果 site.com/page_2 确实有机器人 noindex。它不会循环 site.com/page_1。那是我的问题。

标签: php function file-get-contents connection-refused failed-to-connect


【解决方案1】:

您可以使用 CURL 代替 file_get_contents()

<?php
    $url = 'http://webontwerp-arnhem.nl/contact';         
    $ch = curl_init();        
    curl_setopt($ch, CURLOPT_URL, $url);        
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);        
    $contents = curl_exec ($ch);        
    curl_close ($ch); 

【讨论】:

  • 我试过了,它返回空。
  • 也许尝试更改用户代理
猜你喜欢
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 2018-12-14
  • 1970-01-01
  • 2018-07-21
  • 2020-08-29
  • 1970-01-01
相关资源
最近更新 更多