【发布时间】:2015-10-19 16:05:45
【问题描述】:
我使用以下 php 代码获取页面内容: 但我不知道为什么服务器使用旧浏览器版本访问此页面
$url = $target_domain . 'http://www.facebook.com';
//Download page
$site = file_get_contents($url);
$dom = DOMDocument::loadHTML($site);
if($dom instanceof DOMDocument) {
// find <head> tag
$head_tag_list = $dom->getElementsByTagName('head');
// there should only be one <head> tag
if($head_tag_list->length !== 1) {
throw new Exception('Wow! The HTML is malformed without single head tag.');
}
$head_tag = $head_tag_list->item(0);
// find first child of head tag to later use in insertion
$head_has_children = $head_tag->hasChildNodes();
if($head_has_children) {
$head_tag_first_child = $head_tag->firstChild;
}
// create new <base> tag
$base_element = $dom->createElement('base');
$base_element->setAttribute('href', $target_domain);
// insert new base tag as first child to head tag
if($head_has_children) {
$base_node = $head_tag->insertBefore($base_element, $head_tag_first_child);
} else {
$base_node = $head_tag->appendChild($base_element);
}
echo $dom->saveHTML();
} else {
// something went wrong in loading HTML to DOM Document
// provide error messaging
}
?>
请帮助我如何使用新版本的浏览器访问。
【问题讨论】:
-
f_g_c 不是浏览器。它将通过默认用户代理发送,例如
PHP v5.5或其他。这显然是不是浏览器,所以很多网站都会说“嘿,你需要升级”。 -
@developerwjk 非常感谢
标签: php