Generate Facebook Open Graph Metatexts Dynamically

As the name suggests, I'm trying to generate Facebook Open Graph meta tags dynamically, but I can't get it to work. Is it possible?

UPDATE:

Finally, I started working with @saccharine. The following code works 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 that I entered into the Facebook debugger can now include any dynamic parameters or even none, all or just a choice and in any order: http://mysite.com/index.php?type=restaurant&title=luigis
or that:
http://mysite.com/index.php?locale=de_DE&description=hi&type=bistro

Having achieved this: now I can post actions to the user thread:

 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 !:]

+49
php facebook meta-tags facebook-opengraph
Dec 08 '11 at 13:26
source share
5 answers

First, I want to reiterate that I'm almost sure that your problem is that the URL that you pass to the debugger is not dynamically generated. The url tag essentially acts as a redirector. If this is not the same (i.e., the meta tags on the url meta object match the labels on the URL you are passing) with the URL you are testing, you will not get the desired results.

Meta tag

 <meta property="og:url"> 

must be dynamically generated. The debugger is redirected to the default index page instead of the dynamically generated page.

For example, I assign an identifier to every object that I use, and so I have something like the following

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

I pass this exact URL to the debugger, and so the last page that the debugger gets to will be just that URL.

Also in the following

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

How is a property dynamically generated? Do you remember to install something like the following in your real code?

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

You also seem to put everything in the URL, which is dangerous and can cause huge headaches, which can be a problem here. Instead, insert only one thing, for example? Type = bistro, and then distribute the necessary data from the database.

I would recommend dynamically generating most OG tags based on object_id. Save the appropriate OG information for each object_id, and then distribute them on access. That way, you can also easily extend and edit the tags that you use when upgrading OG.

If you have problems with OG, feel free to post them as new questions, not comments, as I guarantee that other people have the same problem.

+31
Dec 09 2018-11-12T00:
source share

I'm sure Facebook will no longer crawl any URLs with parameters. It always redirects to a stripped down version of the URL.

In the OP example:

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

becomes

http://example.com/index.php

no matter what you do. The closest thing I saw for explanation is this :

 A URL with no session id or extraneous parameters. All shares on Facebook will use this as the identifying URL for this article. 
+4
Apr 24 '15 at 9:53
source share

Yes, it works like a charm, but I need a transcoding. I had to create a new page as follows:

 <!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="og:title" content="<?= urldecode($_GET['title']) ?>" /> <meta property="og:type" content="article" /> <meta property="og:url" content="<?= "http://www.calsots.com".$_SERVER['REQUEST_URI']; ?>" /> <meta property="og:image" content="<?= $_GET['image'] ?>" /> <meta property="og:site_name" content="Calsots.com" /> <meta property="fb:admins" content="MY_APP_ID" /> <meta property="og:description" content="<?= urldecode($_GET['description']) ?>" /> </head> </html> 
+2
Oct 12
source share

When you clicked the Get Code link in your object types, did you try to insert the code that he gave you?
I would try pasting into your network, and then if that works, repeat the html output.
Try this without the DOCTYPE tag.
Heres a sample of what I got and I don't see these tags above: fb: app_id, not sure if that matters.
Also, shouldnt og: url also include variables at the end?

<head prefix = "og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# : http://ogp.me/ns/website# ">

<meta property = "fb: app_id" content = "1234567888">
> <meta property = "og: url" content = "http://mysite.com/index.php?type=MY_APP_NAMESPACE%3Abistro">

+1
Dec 08 '11 at 16:21
source share

For Joomla Opengraph Meta Dynamics:

 <meta property="og:title" content="<?= $title = $this->getTitle(); ?>" /> <meta property="og:type" content="website" /> <meta property="og:url" content="<?= "http://YORUWEBSITE.com".$_SERVER['REQUEST_URI']; ?>" /> <meta property="og:image" content="http://YOURWEBSITE.com/images/stories/BIGIMAGE.jpg" /> <meta property="og:site_name" content="YOURWEBSITE.com" /> <meta property="fb:app_id" content="YOURFACEBOOKAPPIDNUMBER" /> <meta property="og:description" content="<?= $title = $this->getDescription(); ?>" /> 
0
Feb 24 '13 at 15:28
source share



All Articles