How to recognize Facebook User-Agent

后端 未结 11 1360
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 15:35

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.

What is

11条回答
  •  北海茫月
    2020-11-27 15:58

    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
    }
    

    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.

提交回复
热议问题