簡體   English   中英

php 致命錯誤:未捕獲的 ValueError:DOMDocument::loadHTML()

[英]php Fatal error: Uncaught ValueError: DOMDocument::loadHTML()

我正在關注一個教程,該教程展示了如何編寫一個解析網頁並找到所有鏈接的程序。 但是,該程序僅適用於使用 http 的頁面。 每當我嘗試針對具有證書 (https) 的站點運行它時,它都會引發以下錯誤:

致命錯誤:未捕獲的 ValueError:DOMDocument::loadHTML():參數 #1 ($source) 在 C:\xampp\htdocs\froogal\classes\DomDocumentParser.php:14 中不能為空 堆棧跟蹤:#0 C:\xampp \htdocs\froogal\classes\DomDocumentParser.php(14): DOMDocument->loadHTML('') #1 C:\xampp\htdocs\froogal\crawl.php(6): DomDocumentParser->__construct('http:// www.appl...') #2 C:\xampp\htdocs\froogal\crawl.php(18): followLinks('http://www.appl...') #3 {main} 在 C 中拋出: \xampp\htdocs\froogal\classes\DomDocumentParser.php 在第 14 行

DomDocumentParser.php 文件的代碼是:

<?php
class DomDocumentParser {

    private $doc;

    public function __construct($url) {

        $options = array(
            'http'=>array('method'=>"GET", 'header'=>"User-Agent: doodleBot/0.1\n")
            );
        $context = stream_context_create($options);

        $this->doc = new DomDocument();
        @$this->doc->loadHTML(file_get_contents($url, false, $context));
    }

    public function getlinks() {
        return $this->doc->getElementsByTagName("a");
    }

}
?>

crawl.php 的代碼是:

<?php
include("classes/DomDocumentParser.php");

function followLinks($url) {

    $parser = new DomDocumentParser($url);

    $linkList = $parser->getLinks();

    foreach($linkList as $link) {
        $href = $link->getAttribute("href");
        echo $href . "<br>";
    }

}

$startUrl = "http://www.apple.com";
followLinks($startUrl);
?>

在 DomDocumentParser.php 文件中制作
@$this->document->loadHTML('<?xml encoding="UTF-8">'.file_get_contents($url,false,$context));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM