如何识别 Facebook User-Agent [英] How to recognize Facebook User-Agent

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

问题描述

在 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 User-Agent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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