Oct 28 2009

The new Facebook streams explained and simplified (using php).

As we reported a few days ago the way that Facebook feed stories work is changing to a new system.  The new system can be used right away, but the old system will be depreciated (in other words it will stop working) on 20th December 2009.

If you have looked into the new system of feed stories on Facebook it may have left you a little bemused. The example code they give is a little confusing and doesn’t quite work ‘as-is’.

After much experimenting, hitting my head against the proverbial wall and wading through page loads of posts on the developer forum from people who clearly were having similar problems, I came up with some code that works.

Let us look at this example first which requests extended permissions and then posts a feed to a users wall.  Extended permissions is the new feature where your application has to ask the user to grant permission to access or post to their wall, send them email, have offline access, send them email, etc.

The good news is that buried deep in the explanations on the Facebook developer site it turns out that there is still a way to simply pop up a request to post a feed to the users wall without the extended permissions but it is not accessible directly from the php library. However I will show you that code in a moment.

If you haven’t already you will need to download the latest facebook php libraries.  As always these are downloadable from

http://svn.facebook.com/svnroot/platform/clients/packages/facebook-platform.tar.gz

Here is my modified version of the code which I have turned into a demo that asks you for extended permissions and then posts a sample feed to your wall.  You will notice that I only ask for ‘publish stream’ permission since it is all my application needs.  I would strongly recommend that you only ask for what is necessary as users are already going to be a little wary of granting permissions so the fewer you ask for the more likely the user is going to accept.

Here is the code: (there is a downloadable zip file with all the code samples at the end of this article).

php code for Facebook Stream Publish and Extended Permissions.

php code for Facebook Stream Publish and Extended Permissions.

You need to include your appinclude and the JSON encoder scripts at the start of your php file.

If you run the code above it produces the following output:

Facebook streams publish demo in php.

Facebook streams publish demo in php.

In the sample code I have included both the standard FBML code which puts in a text link for the user to click on and have also shown how you can turn this into a form button.

What this has that was missing from the original demonstration code on the Facebook developer site is a check to see if you have the necessary permissions to publish.  If the permissions have not been granted then the form button labeled “grant permission’ is shown.  When a user click on that button or the text link at the top they will be asked if they wish to grant the application permission to post to their wall.

Facebook extended permissions form.

Facebook extended permissions form.

If you want to ask for more permissions then seperate them by commas. For example;

<fb:prompt-permission perms=” read_stream, publish_stream, offline_access, email, status_update, photo_upload, create_event, rsvp_event, sms, video_upload, create_note, share_item. “>Our application needs you to grant permission so it can run correctly.</fb:prompt-permission>

And if you allow permission the feed will then be published to your profile:

Cat feed example.

Cat feed example.

If you want to run this sample for yourself then it is available here: http://apps.facebook.com/extendedfeedsample/

Some key changes that you will have to bear in mind are;

1) If you pass more than one image to the feed only the first image passed will be shown. People will be able to click on a ‘more’ link to see any others.

2) Images will be rendered to 90 x 90 pixels. This is done by Facebook so you don’t have to worry if you pass a larger image, just be aware that it will be shrunk down to fit the new size.

3) A stream story can have a maximum of one action link, and 25 characters in the link. Facebook’s own action links tend to be even shorter than that (“Comment”, “Like”, “Share”, “Become a Fan” are all ten characters or fewer).  “Formatting” characters (for example, leading spaces, pipes, and brackets) will be stripped out to keep in the same simple aesthetic as the rest of Facebook.

4) There are other changes that limit what can be passed to a feed.  This is all covered in the Facebook documentation.

The Pros and Cons of the stream publish method…

The one advantage that you might spot right away is that if a user has granted the correct permission you can post a feed straight to their feed without any annoying pop up box showing at all. That certainly seems to be a massive advantage, but the harsh reality is that people don’t like granting special permissions.

I have a lot of very simple applications like this one: http://apps.facebook.com/mydeathprediction/

Apps like that and fortune cookie applications which are meant to just tell the user some trivial bit of information don’t need to publish anything in the eyes of a user. So they may well refuse to give permission. At which point your whole viral marketing edge has gone.

But there is good news!

There is still a way of popping up a box each time you wish to publish a feed story and you can do it without asking for extended permissions.

It virtually emulates the old template way of doing things except it doesn’t use templates.  If you try my ‘How and When will I die’ application you will see that it pops up a box and it doesn’t ask for extended permissions.  In my app I put a button for the user to click on if they want to publish the story, but you could just as easily (in fact its even easier) just pop up the feed story publish box.

At the end of this article is a downloadable zip file which includes the example above and the code which publishes a feed without extended permissions that I am about to explain.

If you have my ‘How and When will I die’ application then you already have a working example of the code below.

Here is the code that does that taken from a similar application:-

echo ‘I am about to pop up a feed story box please wait…’;
$thefact=”The fact Machine said: Did you know that 2 is more than 1″;
$fimage = $appCallBackUrl.’yourimage.jpg’;
$attachment = array( ‘name’ => ‘I asked the Fact Machine!’, ‘href’ => $appCanvasUrl, ‘caption’ => ‘{*actor*} asked the Fact Machine for a fact.’, ‘description’ => $thefact, ‘properties’ => array(‘Ask the machine’ => array( ‘text’ => ‘Click here to ask the machine.’, ‘href’ => $appCanvasUrl)), ‘media’ => array(array(‘type’ => ‘image’, ‘src’ => $fimage, ‘href’ => $appCanvasUrl)));

$actions=array(array(‘text’ => ‘Try Now!’, ‘href’ => $appCanvasUrl) );

$attachment= json_encode($attachment);
$actions= json_encode($actions);
?>

<script>
var attachment = <?php echo $attachment; ?>;
var myactions = <?php echo $actions; ?>;
Facebook.streamPublish(”, attachment, myactions, ”, ‘Whats on your mind?’);
</script>
<?php
// You can put more php here.
?>

What this code does is format the arrays necessary, JSON encodes them and then outputs some javascript that calls the java version of the stream publish function.  This is what generates the pop up box without having to have extended permissions.  As always you will need to call the configuration file and the JSON code.  You can download this code and the other samples using the link below:

Source code samples for stream publishing and feed forms (105)

Now get out there and create some great viral feeds using the new feed code!

5 Responses to “The new Facebook streams explained and simplified (using php).”

  1. jafarmahmood Says:

    Hi
    I just downloaded the sample.That was simply awesome.

    Can you help me with something? I’m using the “pop up feed story with no ext permissions” version and i want to show more than one image in the feed story.I know it’s possible but not sure how the code will go.I believe you can help me with this.

    Thanks in advance.
    Cheers

  2. Andy Says:

    Sorry for the delay in responding. You can pass multiple images through in the array but only one is shown on the feed. The user has to click the ‘see more’ link to see the other images. This is one of the many changes that Facebook implemented 5th January. There is more on this in the facebook developer wiki.

    Andy

  3. preeti Says:

    Thanks a lot for sharing example, it was of gr8 help!!

  4. Andy Says:

    Glad I could be of help :-)

    Best wishes,

    Andy

  5. gosar.rina Says:

    Thanks a ton for providing this example.. This is truly awesome…

    Thanks!!

Leave a Reply

You must be logged in to post a comment.