DOMDocument :: loadHTMLFile()修改用户代理 [英] DOMDocument::loadHTMLFile() modify user agent

查看:116
本文介绍了DOMDocument :: loadHTMLFile()修改用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im using PHP to load a website in a DOM Tree. Is there a way to modify the user agent that is sent using DOMDocument::loadHTMLFile()?

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

  return $html
}


推荐答案

更改 php.ini 中的 user_agent 值,该值应通过以下方式发送: http流包装器,例如 DOMDocument :: loadHtmlFile(),file_get_contents()等。

Change the user_agent value in php.ini, which should be sent in anything making use of the http stream wrapper like DOMDocument::loadHtmlFile(), file_get_contents(), etc.

$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);

同样可以在Apache .htaccess 通过在服务器配置允许的情况下设置 php_value user_agent

The same can also be accomplished in an Apache .htaccess by setting php_value user_agent if permitted by your server configuration.

这篇关于DOMDocument :: loadHTMLFile()修改用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆