-2
<?php
    class parsedictionary {
        public function _process() {
            $webpage="http://www.oppapers.com/essays/Computerized-World/160871?read_essay";
            $doc=new DOMDocument();
            $doc->loadHTML($webpage);
            echo $doc;
        }
    }
    $obj=new parsedictionary();
    $obj->_process();
?>

我无法获取该页面的内容。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
    <body>
        <p>http://www.oppapers.com/essays/Computerized-World/160871?read_essay</p>
    </body>
</html>

但我需要获取该页面的内容。

4

3 回答 3

2

The DOMDocument class is obviously not a string; you can iterate it, perform operations on it, but it can't just be echoed. Check the documentation to see what you can do with it: http://www.php.net/domdocument

To get the page contents, you can either use file_get_contents or do echo $doc->saveHTML()

Edit: Didn't realize you had another problem in your code; you can just use this instead:

public function _process() {
    return file_get_contents('http://www.oppapers.com/essays/Computerized-World/160871?read_essay');
}
于 2012-04-23T07:24:21.403 回答
1
<?php
    $doc->saveHTML();
?>

奇迹般有效。

于 2013-01-08T14:57:18.173 回答
0

好吧,在这种情况下,错误很明显。_process() 方法无法从一种数据类型转换为另一种数据类型,它需要 String 并且您为其提供 DomDocument。也许您应该首先尝试从 DomDocument 中提取所有文本作为字符串,然后将其发送到 _process() 方法。

于 2012-04-23T07:27:11.037 回答