2012-07-15 3 views
1

PHP를 사용하여 DOM 트리에 웹 사이트를로드하고 있습니다. DOMDocument::loadHTMLFile()을 사용하여 전송 된 사용자 에이전트를 수정하는 방법이 있습니까?DOMDocument :: loadHTMLFile() 사용자 에이전트 수정

function parseThis($url) 
{ 
    $html = new DOMDocument(); 
    $html->loadHtmlFile($url); 

    return $html 
} 

답변

5

변경 등 DOMDocument::loadHtmlFile(), file_get_contents() 같은 HTTP 스트림 래퍼의 어떤하고 사용 보내야 php.ini에서 user_agent 값,

같은도에 의해 아파치 .htaccess에서 수행 할 수 있습니다
$fake_user_agent = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"; 
ini_set('user_agent', $fake_user_agent); 

서버 구성에서 허용하는 경우 php_value user_agent으로 설정하십시오.

+0

재미 있을지 모르지만이 가능성을 알지 못했습니다. – j0k

2

글쎄, 최선의 방법은 콘텐츠를 다른 방식으로 검색하고 문서를로드하는 것입니다. cURL을 사용하여이를 수행 할 수 있습니다.

$useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; 

$ch = curl_init(); 

// set user agent 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

// grab content from the website 
$content = curl_exec($ch); 

// load the content in your dom 
$html = new DOMDocument(); 
$html->loadHTML($content);