Hi there!
Quite a while ago, I started writing my first and own facebook application. I got stuck while trying to publish content to my own profile page and also to my friends’ newsfeeds. I stumbled upon several problems. Either there was no content displayed, it was formatted in a wrong way, it was not published on my friends’ newsfeeds or it even was published on behalf of my friends’ username. It was quite strange to me and I could not find an easy documentation on how to do it the right way.
I played around with Stream.publish, Feed.publishUserAction or Notifications.send – none of those functions did what I wanted them to do. This may be so, because I did not fully understand their purposes.
So, this is my way to do it. I am using the FBJS (FaceBook JavaScript) popup call Facebook.showFeedDialog. This is truely (as far as I’m concerned) the best and easiest way.
First of all, I assume you are familiar with how to build an facebook application from scratch. If not, please read the developer’s documentation.
Step 1: build and register your Feed Template Bundle
- go to the Feed Template Console
- choose your application and hit “Next”
- create a template for your “One Line Story” and hit “Next”
- create a template for your “Short Story” and hit “Next”
- create Action Links and hit “Next”
- hit “Register Template Bundle”
- copy and save the Template Bundle ID given in the popup (you will have to paste it into your application’s code later)
When doing this, you may enter any text or even HyperText in the fields for the One Line or Short Story. You also may enter predefined tags such as {*actor*}
or your own defined tags such as {*foobar*}
. In the Sample Template Data, you may enter the tag definitions and display a preview. What you enter there and at this very moment has no effect on the content displayed within your application later! That’s just for previewing purposes. The real content will be defined later in your application’s script. Remember that! Some Examples for your Sample Template Data at this point may be:
{"foobar": "This is a foobar text"}
Or a bit more complex:
{"foolink": "<a href="http://www.google.com/">Google</a>",</p> <p>"images":[{"src":"http://domain.com/image1.jpg", "href":"<a href="http://www.facebook.com/">http://www.facebook.com</a>"},</p> <p>{"src": "http://anotherdomain.com/image2.jpg", "href":"<a href="http://www.facebook.com/">http://www.facebook.com</a>"}]}
You see, e.g . the {*images*}
tag is defined as some sort of array consisting of multiple SRC and HREF values. Attention: the special tag {*images*}
is displayed automatically on Short Stories, you don’t need to call them!
2. write your application’s content
<?PHP</p> <p>require_once 'facebook-platform/php/facebook.php';</p> <p>$appid = 'xxxxxxxxx'; // your app ID</p> <p>$appapikey = 'XXXXXXXXXXXXXXXX; // your app key</p> <p>$appsecret = 'XXXXXXXXXXXXXXXX'; // your app secret</p> <p>$template_bundle_id = 'xxxxxxxxxxxxxxxx'; // the ID you got in Step 1</p> <p>$facebook = new Facebook($appapikey, $appsecret);</p> <p>$user_id = $facebook->require_login();</p> <p>echo "<p>Hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>";</p> <p>?></p> <p><script type="text/javascript"><!--</p> <p> var template_bundle_id ="";</p> <p> var url="http://www.google.com/";</p> <p> var user_message_prompt = "Share this URL with your friends?";</p> <p> var user_message = {value: "wants to share a greate website."};</p> <p> var image ="http://www.google.com/intl/en_ALL/images/logo.gif";</p> <p> var template_data = {"url": url,</p> <p> "images": [{'href': url, 'src' : image}]};</p> <p> Facebook.showFeedDialog(template_bundle_id, template_data, '', '', '', user_message_prompt, user_message);</p> <p>// --></script></p> <p>
This is it.
Every time you call your application’s page, you’ll get a facebook popup asking you to display the content on your profile and even in your friends’ newsfeeds. Of course, it does not make any sense to share some data every time you call your application’s canvas page, so you may want to add some more code to your application.
The text to be shared is the text you entered when registering the Template Bundle in Step 1. If you get an error, please make sure the defined variables (such as “url” or “images”) are valid and do exist. In other words: whatever you call in your Template Bundle, it has to be defined in your script.
Of course, you may generate the content dynamically from a database or any other source.
I hope this HOWTO helped.
Bye, T.