[英]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
. 您可能要使用stristr
或regex
代替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.