简体   繁体   English

为什么我不能摆脱Facebook用户代理?

[英]Why i can't get rid the Facebook user-agent?

I'd like to use some permalink slug that allow the users to share the link (url.com/artist/songtitle) with it's Facebook pictures, url, description, and so on (Which is redirect the users to url.com/#/artist/songtitle). 我想使用一些永久链接标记,允许用户与Facebook图片,URL,描述等共享链接(url.com/artist/songtitle)(将用户重定向到url.com/# / artist / songtitle)。 So i decided to showing the OG meta to Facebook user-agent and separate it from the redirector. 因此,我决定向Facebook用户代理显示OG元数据,并将其与重定向器分开。

我的密码

But, the problem come when i use the Facebook Debug Tools and try to fetch it . 但是,当我使用Facebook调试工具并尝试获取时,问题就来 The crawlers wasn't caught by my user-agent separator. 搜寻器没有被我的用户代理分隔符捕获。 为什么???

Im using, this code to detect Facebook crawlers. 我正在使用此代码来检测Facebook爬虫。 Any idea to fix this problem? 有解决这个问题的主意吗?

strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit/") || strpos($_SERVER["HTTP_USER_AGENT"], "Facebot"

You may want to use stristr or a regex instead of strpos . 您可能要使用stristrregex代替strpos As it is right now, your code won't match FacebookExternalHit , because it contains Capital Letters and strpos function is CaseSenSiTive . 目前,您的代码与FacebookExternalHit不匹配,因为它包含大写字母,而strpos函数为CaseSenSiTive


Facebook User-Agents are: Facebook用户代理是:

 FacebookExternalHit/1.0 FacebookExternalHit/1.1 facebookexternalhit/1.0 (+http://www.facebook.com/externalhit_uatext.php) facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php) facebookexternalhit/1.0 (+https://www.facebook.com/externalhit_uatext.php) facebookexternalhit/1.1 (+https://www.facebook.com/externalhit_uatext.php) 

I'm using the code below to detect FB User-Agent in PHP and it works as intended: 我正在使用下面的代码来检测PHP中的FB User-Agent,它可以按预期工作:

$agent = $_SERVER['HTTP_USER_AGENT'];
if(stristr($agent, 'FacebookExternalHit')){
    //Facebook User-Agent
}else{
    //Other User-Agent
}

Update: 更新:

In order to know which user agents FB is using, try saving the user agent requests to a file, ie: 为了知道FB正在使用哪个用户代理,请尝试将用户代理请求保存到文件中,即:

Example code to save UA requests: 保存UA请求的示例代码:
Create a file named ua_log.txt with write permissions . 创建一个具有写许可权的名为ua_log.txt的文件。

<?php
$agent = $_SERVER['HTTP_USER_AGENT'];
file_put_contents("$agent\n", "ua_log.txt", FILE_APPEND);    

Use the FB user agents on ua_log.txt to create your filter. 使用ua_log.txt上的FB用户代理创建过滤器。

Figure it out. 想办法。 Be careful when you're writing og:url meta, the crawlers would like to follow it. 当您编写og:url meta时,请小心,爬虫希望遵循它。 So, make sure the page you're write in og:url is correct. 因此,请确保您在og:url中编写的页面正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何卷曲范围和用户代理 - how i can curl Range and User-Agent 是否有模仿User-Agent字符串的iPhone模拟器,所以我可以使用php-mobile-detect? - Is there an iPhone emulator that mimics User-Agent strings so I can use php-mobile-detect? 使用Requests for PHP库,如何设置自定义用户代理? - Using the Requests for PHP library, how can I set a custom user-agent? PHP - 为什么我不能摆脱这个会话id cookie? - PHP - why can't I get rid of this session id cookie? PHP-从User-Agent获取确切的iOS版本 - PHP - Get exact iOS version from User-Agent Zend_Soap_Client可以设置自定义用户代理吗? - Can Zend_Soap_Client set custom user-agent? 我应该为Simplepie设置自定义用户代理吗 - Should I set up a custom user-agent for Simplepie 在没有浏览器生成的用户代理的情况下,如何通过 cURL 将事件数据发送到 Google Measurement Protocol? - How can I send event data to Google Measurement Protocol via cURL without a browser generated user-agent? 模拟Facebook iOS App登录,正在发送特殊用户代理? - Emulating the Facebook iOS App login, Special user-agent being sent? 我无法获取用户facebook的会话,getUser返回0-Codeigniter - I can't get the session of user facebook, getUser return 0 - codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM