PHP更改DOM用户名 [英] PHP change DOM useragent

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

问题描述

我有这个简单的代码来获取任何页面的标题

I have this simple code to get the title of any page

<?php
    $doc = new DOMDocument();
    @$doc->loadHTMLFile('http://www.facebook.com');
    $xpath = new DOMXPath($doc);
    echo $xpath->query('//title')->item(0)->nodeValue."\n";
?>

在我尝试但不在Facebook的所有页面上都可以正常工作。

It is working fine on all pages that I have tried but not in Facebook.

当我在Facebook上尝试不显示欢迎来到Facebook - 登录,注册或了解更多,但它显示更新浏览器| Facebook

When I try in Facebook it is not showing Welcome to Facebook - Log In, Sign Up or Learn More, but it is showing Update Your Browser | Facebook.

我觉得useragent有问题。那么有一种方法可以改变用户端,还是有其他解决方案?

I think there is a problem with useragent. So is there a way to change the useragent or is there any other solution for this?

推荐答案

您可以设置用户代理php.ini,不需要卷曲。在加载DOMDocument之前,请使用下面的代码

You can set the user agent in php.ini, without the need for curl. Just use the below lines before you load the DOMDocument

$agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
ini_set('user_agent', $agent);

然后你的代码:

$doc = new DOMDocument();
@$doc->loadHTMLFile('http://www.facebook.com');
$xpath = new DOMXPath($doc);
echo $xpath->query('//title')->item(0)->nodeValue."\n";

这篇关于PHP更改DOM用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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