simple_html_dom.php解析器无法正常工作

时间:2012-07-12 10:16:29

标签: php simple-html-dom dom

我下载了simple_html_dom.php文件并将其上传到我的网络服务器,我立即用一个简单的脚本对其进行了测试,但是它没有工作,或者至少我认为它没有,因为它没有&输出任何东西。

这是脚本:

<?php
require('simple_html_dom.php');

// Retrieve the DOM from a given URL
$html = file_get_html('http://davidwalsh.name/');

// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e) {
    echo $e->innertext . '<br>';
}
?>

我从这个网站http://davidwalsh.name/php-notifications获得了剧本,并在其他网站上提供了相同的内容,因此我不明白为什么它不会输出任何内容。

我的猜测是该脚本无法从其他网站检索任何数据,例如我遇到的问题:Retrieving cross-domain data php

如果是这样的话,有没有办法避免这个问题?

在BIno Carlos&#39;在他陈述之前回答我之前链接的问题&#34;它不是真正的跨域问题,因为你是从服务器加载数据而不是浏览器&#34;所以可能有一种方法可以加载来自浏览器的数据?

所以,正如user868766在他的回答中所建议的那样,我尝试了两个没有报告任何错误的ini_set方法,所以看起来这个脚本看起来很好。当我在$ html上尝试print_r()方法时输出如下:

simple_html_dom Object ( [root] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [callback] => [lowercase] => 1 [pos:protected] => 0 [char:protected] => [size:protected] => 0 [cursor:protected] => 1 [parent:protected] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [token_blank:protected] => [token_equal:protected] => =/> [token_slash:protected] => /> [token_attr:protected] => > [self_closing_tags:protected] => Array ( [img] => 1 [br] => 1 [input] => 1 [meta] => 1 [link] => 1 [hr] => 1 [base] => 1 [embed] => 1 [spacer] => 1 ) [block_tags:protected] => Array ( [root] => 1 [body] => 1 [form] => 1 [div] => 1 [span] => 1 [table] => 1 ) [optional_closing_tags:protected] => Array ( [tr] => Array ( [tr] => 1 [td] => 1 [th] => 1 ) [th] => Array ( [th] => 1 ) [td] => Array ( [td] => 1 ) [li] => Array ( [li] => 1 ) [dt] => Array ( [dt] => 1 [dd] => 1 ) [dd] => Array ( [dd] => 1 [dt] => 1 ) [dl] => Array ( [dd] => 1 [dt] => 1 ) [p] => Array ( [p] => 1 ) [nobr] => Array ( [nobr] => 1 ) ) [doc:protected] => [noise:protected] => Array ( ) )

1 个答案:

答案 0 :(得分:0)

您发布的代码对我来说很好。

认为你可以尝试这些,将下面的行放在脚本之上:

    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 1);

同时检查$ html的print_r

    print_r($html);

希望得到这个帮助。