2014-07-14 1 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가 "head"인 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); 
?> 

나는 대신이 예제처럼