Category: Facebook

facebook data storage api can really be the replacement of memcache

why not! all you need is a fast-n-furious caching storage for your facebook application which stores values against a key, same like a hash table. facebook data storage api does the same for you. using the batch api in facebook rest client you can seriously think it as an alternative of memcache. there are many developers who cache the data using memcache. memcache is definitely the state of the art caching solution for those who can afford hosting their application in a dedicated or vps server and knows how to install and successfully manage it. it. in other hand, facebook’s data storage api is an excellent solution for those who really want to make use of this awesome development platform without minimum hassle. all you have to do is remember some api and thats it.

lets see how you can use the facebook data storage api.

1. create the object manually (not using api)
an object in data storage api stands for something like a table. it contains some properties (columns) and their values(field values) stored against a key (think about a primary id). using the application control panel you can easily create such an object.

a. visit http://www.facebook.com/developers/apps.php?ret=2
b. select your application and click the link “http://www.facebook.com/developers/dsadmin.php?app_id={your application id}”

from this page you can create an object and create the properties. lets consider that the object name is ‘comics’ and the properties are “comicname[string]” and “comicurl[text/blob]”.

2. insert data into this object
using the storage api you can insert data into this object against a hash value. for example, lets have a look at the following piece of code

<?php
//after initializing the app client
//set value to the properties in a batch process
$facebook_api_client->begin_batch();
$facebook_api_client->data_setHashValue("comics","nancy","Nancy And Sluggo","comicname");
$facebook_api_client->data_setHashValue("comics","nancy","http://url_of_this_comic","comicurl");
$facebook_api_client->end_batch();
?>

the example showed above will set the value of two properties in “comics” object against the common key “nancy”.

setting up the values to the properties of an object in a batch process will speed up your application execution time by running all the api calls in that batch at once. the same thing applies if you want to retrieve the values of those properties.

3. retrieve the values
now when you need to re use the value of that object in your facebook application, you can do it like this

<?php
//retrieve the values of two properties "comicname" and 'comicurl"
$facebook_api_client->begin_batch();
$comicname = $facebook_api_client->data_getHashValue("comics","nancy","comicname");
$comicurl = $facebook_api_client->data_getHashValue("comics","nancy","comicurl");
$facebook_api_client->end_batch();
?>

thats a tiny introduction to the storage api of facebook. there are lots more you can do by defining associations (much like table relations between two tables) and other available apis in this category. i will focus some of them in my upcoming blog posts.

before ending, revise the title of this blog post as “facebook data storage can really be the replacement of memcache for facebook application developers”.

Unexpected return value from Facebook FQL.query via PHP REST Lib

While working with FQL I suddenly found a bug in one of our applications. The method which we used to count number of friends of a specific user who has added that application was returning 1 when there is no friend actually installed it। Using the following FQL you can easily find that. (FQL doesn’t support “count” – so you cant use it)

SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$userid})

The PHP code may look like the following one

function getTotalFriends($userid)
{
global $facebook, $mysql;
$Users = $facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$userid})");
$NumberOfUsers = count(trim($Users));
return $NumberOfUsers;
}

So what would you expect when there is no friend who has added this application? Definitely the FQL query should return an empty array. So that counting elements of that array tells you that you have “0” friends. But actually it returns a white space. What’s the difference between an empty array and a white space? l

count(array()) returns 0
count (” “) returns 1

So even when you dont have any friends, your method actually returns you “1”. The return result is not expected. What does your common sense says? Either the method should return false or an empty array. Not a white space.

Here’s the correction


function getTotalFriends($userid)
{
global $facebook, $mysql;
$Users = $facebook->api_client->fql_query("SELECT uid FROM user WHERE has_added_app = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$userid})");
$NumberOfUsers = is_array($Users)?count($Users):0;
return $NumberOfUsers;
}