DOMDocument :: loadHTMLFile()修改用户代理

Joe*_*oey 2 php user-agent domdocument

我使用PHP加载DOM树中的网站.有没有办法修改使用发送的用户代理DOMDocument::loadHTMLFile()

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

  return $html
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*ski 6

更改user_agentphp.ini,应该在使用http流包装器等的任何内容中发送DOMDocument::loadHtmlFile(), file_get_contents(),等等.

$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);
Run Code Online (Sandbox Code Playgroud)

如果您的服务器配置允许,也可以.htaccess通过设置在Apache中完成相同的操作php_value user_agent.