2012-07-12 112 views
0

我下載的文件simple_html_dom.php並將其上傳到我的Web服務器,我立即用一個簡單的腳本測試它,但它不會工作,或者至少我想這不會因爲它不輸出任何東西。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對我之前鏈接的問題的回答中,他聲明「這不是一個真正的跨域問題,因爲您正在從服務器而不是瀏覽器加載數據」,所以可能會有一種方法來加載來自瀏覽器的數據?

所以,通過user868766在他的回答提出,我想這兩個函數ini_set方法沒有報告任何錯誤,所以它看起來像腳本工作顯然就好了。當我試圖在$的的print_r()方法HTML時,它的輸出如下:

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 ()) 
+0

請定義_won't WORK_。 – Florent 2012-07-12 10:18:17

+1

您是否已完成** ANY **基本調試,例如轉儲$ html的值以確保您收到了一些東西? – 2012-07-12 10:19:07

+1

另外,爲什麼不使用內置的DOMDocument功能? http://www.ibm.com/developerworks/library/os-xmldomphp/ – 2012-07-12 10:19:38

回答

0

您發佈的代碼是爲我工作的罰款。

認爲你可以嘗試這些,下面放線對腳本的頂部:

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

還要檢查$ HTML

print_r($html); 

希望這有助於中的print_r。

+0

謝謝,前兩個沒有輸出任何東西,看起來腳本可以正常工作。現在我將更新print_r()方法的輸出。 – BigCola 2012-07-12 10:47:56