如何识别Facebook用户代理 [英] How to recognize Facebook User-Agent

查看:393
本文介绍了如何识别Facebook用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FB上分享我的一个页面时,我想显示不同的内容。问题是,我不喜欢使用og:元素,而是识别FB用户代理。

When sharing one of my pages on FB, I want to display something different. Problem is, I prefer not to use the og: elements, but to recognize FB user-agent.

是什么?我找不到它。

谢谢

推荐答案

的用户代理字符串,请查看此处。截至2015年9月,最常用的是 facebookexternalhit / * Facebot 。由于您没有说出您想要识别用户代理的语言,我无法告诉您更多信息。如果你想在PHP中识别Facebook机器人,请使用

For list of user-agent strings, look up here. The most used, as of September 2015, are facebookexternalhit/* and Facebot. As you haven't stated what language you're trying to recognize the user-agent in, I can't tell you more information. If you do want to recognize Facebook bot in PHP, use

if (
    strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit/") !== false ||          
    strpos($_SERVER["HTTP_USER_AGENT"], "Facebot") !== false
) {
    // it is probably Facebook's bot
}
else {
    // that is not Facebook
}

更新:Facebook已将 Facebot 添加到其可能的用户代理字符串列表中,所以我更新了我的代码来反映这个变化。此外,代码现在更可预测未来的变化。

UPDATE: Facebook has added Facebot to list of their possible user-agent strings, so I've updated my code to reflect the change. Also, code is now more predictible to possible future changes.

这篇关于如何识别Facebook用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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