动态生成Facebook Open Graph元标记 [英] Generating Facebook Open Graph meta tags dynamically

查看:864
本文介绍了动态生成Facebook Open Graph元标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题意味着我试图动态地生成Facebook Open Graph元标记,但我无法使其工作。甚至有可能吗?



更新:



最后我得到它的帮助@糖精。以下代码适用于我:

 <?php 

$ params = array() ;
if(count($ _ GET)> 0){
$ params = $ _GET;
} else {
$ params = $ _POST;
}
//默认
if($ params ['type'] ==)$ params ['type'] =restaurant;
if($ params ['locale'] ==)$ params ['locale'] =en_US;
if($ params ['title'] ==)$ params ['title'] =default title;
if($ params ['image'] ==)$ params ['image'] =thumb;
if($ params ['description'] ==)$ params ['description'] =default description;

?>

<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional。 DTD>
< html xmlns =http://www.w3.org/1999/xhtmllang =enxml:lang =en>
< head prefix =og:http://ogp.me/ns# fb:http://ogp.me/ns/fb# MY_APP_NAME_SPACE:http://ogp.me/ns/fb/ MY_APP_NAME_SPACE#>
< title>< / title>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>

<! - 打开图表元标记 - >
< meta property =fb:app_idcontent =MY_APP_ID/>
< meta property =og:site_namecontent =meta site name/>
< meta property =og:urlcontent =http://mysite.com/index.php?type=<?php echo $ params ['type'];?>& locale =<?php echo $ params ['locale'];?>& title =<?php echo $ params ['title'];?>& image =<?php echo $ params [ image'];?>& description =<?php echo $ params ['description'];?>/>
< meta property =og:typecontent =MY_APP_NAME_SPACE:<?php echo $ params ['type'];?>/>
< meta property =og:localecontent =<?php echo $ params ['locale'];?>/>
< meta property =og:titlecontent =<?php echo $ params ['title'];?>/>
< meta property =og:imagecontent =http://mysite.com/img/<?php echo $ params ['image'];?> .png/>
< meta property =og:descriptioncontent =<?php echo $ params ['description'];?>/>

< / head>
< / html>

现在我加入Facebook调试器的URL可以包含任何动态参数,甚至没有,全部或只有一个选择,按照这样的任何顺序:

http://mysite.com/index.php?type=restaurant&title=luigis

或这个:

http://mysite.com/index.php?locale=de_DE&description=hi&type=bistro



完成:我现在可以向用户的流发布操作:

  function postRestaurant(){
FB.api('me / MY_APP_NAMESPACE:have_lunch?\
start_time = 2000-12-12T04:00:00& \
expires_in = 7200& \
restaurant ='+ encodeURIComponent(getRedirectURI()+'?type = restaurant'+'& description = arnold'+'& title = st单独'),'post',function(response){
if(!response || response.error){
console.log('postRestaurant:Error occured =>'+ response.error.message);
} else {
console.log('postRestaurant:Post was successful!Action ID:'+ response.id);
}
});
}

像一个魅力一样工作! :]

解决方案

首先,我想重申,我几乎是积极的,你的问题是由于你的url你正在传入调试器不是动态生成的。网址标签基本上作为重定向器。除非是完全相同的(意味着url元对象上的元标记与您传递的url中的元标记相同)作为您正在测试的URL,否则您将无法获取所需的结果。 p>

元标记

 < meta property =og:url > 

需要动态生成。调试器被重定向到您的默认索引页,而不是动态生成的页面。



例如,我为每个正在使用的对象分配一个id,所以我有一些类似于以下的

 < meta property =og:urlcontent =http://mysite.com/ index.php?id =<?php echo $ _GET ['id']?>/> 

我将精确的URL传入调试器,因此调试器所在的最后一页将是



另外,在以下

  meta property =og:typecontent =/> 

该属性是如何动态生成的?你记得在你的实际代码中设置如下吗?

 < meta property =og:typecontent = <?php echo _GET ['type]?>/> 

你似乎正在将所有内容都推入url,这是危险的,可能会导致巨大的头痛,可能是这里的问题。相反,只推一个东西,例如?type = bistro,然后从DB传播必要的数据。



我建议基于object_id动态生成大多数OG标签。存储每个object_id的相关OG信息,然后在访问时传播它们。这样,您也可以轻松扩展和编辑OG更新时使用的标签。



如果您对OG有任何问题,您应该毫不犹豫地将其发布为新问题,而不是发表评论,因为我保证其他人也有同样的问题。

As the title implies I'm trying to generate Facebook Open Graph meta tags dynamically, but I can't get it working. Is it even possible?

UPDATE:

Finally I got it working with the help of @saccharine. The following code is working for me:

<?php

$params = array();
if(count($_GET) > 0) {
    $params = $_GET;
} else {
    $params = $_POST;
}
// defaults
if($params['type'] == "") $params['type'] = "restaurant";
if($params['locale'] == "") $params['locale'] = "en_US";
if($params['title'] == "") $params['title'] = "default title";
if($params['image'] == "") $params['image'] = "thumb";
if($params['description'] == "") $params['description'] = "default description";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# MY_APP_NAME_SPACE: http://ogp.me/ns/fb/MY_APP_NAME_SPACE#">
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

        <!-- Open Graph meta tags -->
        <meta property="fb:app_id" content="MY_APP_ID" />
        <meta property="og:site_name" content="meta site name"/>
        <meta property="og:url" content="http://mysite.com/index.php?type=<?php echo $params['type']; ?>&locale=<?php echo $params['locale']; ?>&title=<?php echo $params['title']; ?>&image=<?php echo $params['image']; ?>&description=<?php echo $params['description']; ?>"/>
        <meta property="og:type" content="MY_APP_NAME_SPACE:<?php echo $params['type']; ?>"/>
        <meta property="og:locale" content="<?php echo $params['locale']; ?>"/>
        <meta property="og:title" content="<?php echo $params['title']; ?>"/>
        <meta property="og:image" content="http://mysite.com/img/<?php echo $params['image']; ?>.png"/>
        <meta property="og:description" content="<?php echo $params['description']; ?>"/>

    </head>
</html>

The url I'm putting into the Facebook debugger now can include any of the dynamic parameters or even none, all or only a selection and in any order like so:
http://mysite.com/index.php?type=restaurant&title=luigis
or this:
http://mysite.com/index.php?locale=de_DE&description=hi&type=bistro

Having that accomplished: I can now publish actions to the user's stream:

function postRestaurant() {
    FB.api('me/MY_APP_NAMESPACE:have_lunch?\
    start_time=2000-12-12T04:00:00&\
    expires_in=7200&\
    restaurant=' + encodeURIComponent(getRedirectURI() + '?type=restaurant' + '&description=arnold' + '&title=stalone'), 'post', function (response) {
        if (!response || response.error) {
            console.log('postRestaurant: Error occured => ' + response.error.message);
        } else {
            console.log('postRestaurant: Post was successful! Action ID: ' + response.id);
        }
    });
}

Works like a charm! : ]

解决方案

First, I want to reiterate that I am almost positive that your problem is due to the fact that the url you are passing into the debugger is not dynamically generated. The url tag essentially acts as a redirector. Unless it's the exact same (meaning the meta tags on the url meta object is the same as those on the url you are passing in) as the url you are testing, you won't get the results you're looking for.

The meta tag

<meta property="og:url"> 

needs to be dynamically generated. The debugger is being redirected to your default index page instead of the dynamically generated page.

For example, I assign an id to every object I'm using, and so I have something like the following

<meta property="og:url" content="http://mysite.com/index.php?id=<?php echo $_GET['id'] ?>"/> 

I pass in that exact url into the debugger, and thus the final page the debugger lands on will be that exact url.

Also, in the following

<meta property="og:type" content=""/>

how is the property being dynamically generated? Did you remember to set in your actual code something like the following?

<meta property="og:type" content="<?php echo _GET['type] ?>"/>

You also appear to be shoving everything into the url, which is dangerous and can cause huge headaches, which might be the issue here. Instead, shove only one thing , eg ?type=bistro and then propagate the necessary data from the DB.

I would recommend dynamically generating most OG tags based on an object_id. Store the relevant OG info for every object_id, and then propagate them when accessed. This way, you can also easily expand and edit the tags you use when OG is updated.

If you have problems with OG you shouldn't hesitate to post them as new questions instead of comments as I guarantee other people also have the same problem.

这篇关于动态生成Facebook Open Graph元标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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