Online Book Reader

Home Category

Facebook Cookbook - Jay Goldman [146]

By Root 767 0
if otherwise. Stories that were previously published with the deactivated template will still display in the News and Mini-Feeds, but you won’t be able to publish to it anymore.

Publishing News and Mini-Feed Stories


Problem


I want to publish stories about my users’ activities in my app so that their friends see them and become engaged.

Solution


Use the Feed.publishUserAction() method:

$template_bundle_id = 12345678901;

$template_data = array(

'nap-type'=>'Super Disco Nap',

'status'=>'fabulous',

);

$target_ids = '12345,67890';

$result = $facebook->api_client->feed_publishUserAction

($template_bundle_id, json_encode($template_data), $target_ids);

You can optionally include a fourth parameter, body_general, to add additional body content to extend short story format posts. If these stories are being aggregated, Facebook will include one of the body_general parameters in the aggregated story. See Three Story Sizes: Working with Template Bundles for more information about template bundles.

Discussion


Your app used to be limited to calling this function once every 12 hours for every user other than those listed as developers for your app (who will always see every story that you publish for testing purposes). At the time this book was going to press, it remained unclear as to whether that would continue to be the case with the Profile redesign.

Note that the template_data needs to be JSON-encoded when you pass it into the Feed.publishUserAction() method.

ESCAPING JSON-ENCODED URLS

The JSON-encoding step is easy if you’re working in PHP 5.2 (or newer), since it includes a JSON encoder. This can become a particular pain with URLs that you might include in the template_data array, which need to have all the slashes escaped. If, for example, you have a token called {*photo*} and you need to substitute in the actual tag linking to http://flickr.com/photos/someuser/12345/, you would need to include it as:

{"photo":"Flickr<\/a>"}

Take a look at the PHP documentation for json_encode(), which makes that a whole lot easier:

$titleData = json_encode(array('photo => '"http://flickr.com/photos/someuser/12345/">Flickr', ));

If you’re working in an earlier version of PHP, check out the Zend JSON encoder at http://framework.zend.com/manual/en/zend.json.html. Developers working in other languages should find a similar library.

Stories may or may not show up in users’ News Feeds, depending on a very complex and highly secretive algorithm that the Facebook team keeps locked up in a cage deep underground their Palo Alto lair. Thankfully, they released some details about how this works back in December 2007, which you can still read at http://wiki.developers.facebook.com/index.php/FeedRankingFAQ (note that this might be out-of-date or exactly accurate—such is the joy of proprietary algorithms).

Feed.publishUserAction() will return either true (1) on success, false (0) on a permissions error, or one of the other Facebook error codes (see Error Codes for more information).

Story Aggregation


Problem


I’ve heard that Facebook aggregates stories together, but I don’t know what that means.

Solution


If your app is growing in popularity and you have multiple users who are connected through part of the social graph, Facebook will aggregate the stories from your app about a given user’s friends, and that aggregated story will have a much higher chance of showing up in their feed. You’ve likely seen this happen with Facebook’s own stories, particularly about things such as Profile picture changes. Any of your stories that are published using the same registered template bundle can be aggregated automatically.

Discussion


Let’s take a look at an example of aggregation. If our handy old Super Disco Napping application developed the ability to report on the post-napping brunch adventures of its users, it might publish a News story something like the one shown in Figure 9-11.

Figure 9-11. News story, non-aggregated

Return Main Page Previous Page Next Page

®Online Book Reader