如何让Facebook评论从1个URL预览不同的开放图形标签

时间:2017-06-07 02:17:21

标签: php html opengraph

我看到那些Facebook帖子询问“你是哪个漫威超级英雄?”或者“你是哪个口袋妖怪?”然后告诉你在帖子的评论中键入一个URL,它会在评论预览窗格中给你一个答案,比如“你是蜘蛛侠。喜欢吗?分享这个。”

昨天我尝试做类似的事。我选择了神奇女侠电影作为主题。 我已将一个简单的网页编码为一个简单的“你是哪个神奇女子电影角色?”页。它将自动为您自动选择一个字符。所以基本上,我有一个PHP文件。在其中,我放了一个php数组,分配3个不同的html文件(Steve,Wonder Woman& Antiope)。然后随机化器代码将自动从0,1,2中选择阵列插槽的编号。并使用readfile()作为您的角色来获取html页面。我放的唯一额外的东西是.htaccess将test_wonderwoman.html重定向到php页面。因此,每当您访问test_wonderwoman.html页面(顺便说一句,它不存在)时,它将自动指向test_wonderwoman.php,一旦到达目的地,当刷新浏览器时,它将获得一个随机字符。有时你可能连续两次或三次获得相同的字符,因为它是随机分配的,我现在只有3个字符。

我可以在网页本身上使用它。但我无法让它在Facebook上运作。在Facebook中,每当有人将URL作为页面帖子或帖子下的评论发布时,它应该给出角色的预览窗格和描述。这是因为在HTML标签中添加了Facebook Open Graph元标记。我被告知Facebook webcrawler将从我的免费虚拟主机服务器访问该页面,这就是为什么我尝试使用PHP告诉我的服务器在请求时为FB爬虫提供一个随机的html页面。

它无法在Facebook上运行。它没有随机化。它反复显示一个字符。另外因为我的网页是新的,并且没有多少人分享链接,FB Security一直要求我输入随机代码,否则会阻止我发布链接。

通过使用FB OG调试控制台并使用刮刀工具,它确实告诉我他们的抓取工具确实随机看到了不同的html页面。例如,它确实可以查看和获取wonderwoman.html页面,但它仍然会在预览窗格中显示Steve Trevor页面。

注意:我修改了.htaccess页面。因为我以后想的是我可以有一个更好的命名URL指向test_wonderwoman.php页面。现在它正在将https://oskuek.000webhostapp.com/test_wonderwoman.html重定向到那个。

我无法弄清楚出了什么问题。请问你能帮帮我吗?感谢。

.htaccess 
# HTID:1508814: DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES BELOW
php_value display_errors 1
# DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES ABOVE HTID:1508814:

RewriteEngine On
RewriteBase /
RewriteRule ^test_wonderwoman\.html?$ /test_wonderwoman.php [NC,R,L]



test_wonderwoman.php page
<?php 
$RandomList = array(); // Leave this line as is.
// Edit next lines for the file locations to be inserted randomly.
$RandomList[] = "/wonderwoman.html";
$RandomList[] = "/antiope.html";
$RandomList[] = "/stevetrevor.html";

// Leave next line as is.
readfile($_SERVER['DOCUMENT_ROOT'].$RandomList[rand(0,2)]);

?>


stevetrevor.html page
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>You are Steve Trevor</title>
    <meta name="description" content="Like it? share with friends" />

    <meta property="og:type" content="website" />
    <meta property="og:url" content="oskuek.000webhostapp.com" />
    <meta property="og:title" content="You are Steve Trevor" />
    <meta property="og:description" content="Like it? share with friends" />

    <meta property="og:image:width" content="400" />
    <meta property="og:image:height" content="400" />
    <meta property="og:image" content="https://oskuek.000webhostapp.com/images/stevetrevor400x400.jpg" />

</head>
<body>
<img src="images/stevetrevor400x400.jpg">
</body>
</html>

0 个答案:

没有答案
相关问题