2014-07-14 52 views
0

下面是我寫的一些php代碼。它主要基於文檔。 這顯然是使用簡單的html dom 問題是它沒有真正的工作,我不知道爲什麼。簡單的html dom div下載問題

<?php 
include("simple_html_dom.php"); 
$context = stream_context_create(); 
stream_context_set_params($context, array('user_agent' => "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36")); 
$html = file_get_html('http://www.ask.fm', 0, $context); 
$elem = $html->find('div[id=heads]', 0); 
var_dump($elem); 
?> 

我想要的是設置useragent,我試圖在這句話之上做。然後我想用id「頭像」下載div。這並不多,但我無法以任何方式解決問題。

+1

使用捲曲有用的,而不是像在這個例子:http://stackoverflow.com/a/21186309/1519058 – Enissay

+1

什麼叫下載DIV是什麼意思?你已經有了元素?最後的輸出是什麼? – user1978142

+0

問題是,這段代碼不應該工作,但它沒有。通過下載div我的意思是從其他網站獲取div並將其回顯出來 –

回答

1
<?php 
include "simplehtmldom_1_5/simple_html_dom.php"; 
function curl($url) 
{ 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    // you may set this options if you need to follow redirects. Though I didn't get any in your case 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
    $content = curl_exec($curl); 
    curl_close($curl); 
    return $content; 
}  

$html = str_get_html(curl("http://www.ask.fm")); 
echo $elem = $html->find('div[id=heads]', 0); 
?> 

我認爲這是給你的