Boost your Facebook Request

Boost your Facebook Request

The Facebook API is a platform for building applications that are available to the members of the social network of Facebook. The API allows applications to use the social connections and profile information to make applications more involving, and to publish activities to the news feed and profile pages of Facebook, subject to individual users privacy settings. With the API, users can add social context to their applications by utilizing profile, friend, Page, group, photo, and event data. The API uses RESTful protocol and responses are localized and in XML format.

www.programableweb.com

 

Nowadays there are so many API, so that with Facebook, when we want to grow our App or website with Facebook we will face Facebook API. And now we’ll study about Facebook API and how to boost our request, I assume you already know and ever use Facebook API and here we’ll use gem “koala” (www.github.com) to make the request easier. Again I assume you already create an App to authenticate Facebook user and save Facebook access token in you database. Okay here we go.

We should know that Facebook has Graph API and Facebook Query Language (FQL) which many developers use to develop their App and another important thing we should know that’s Facebook Explorer (www.developers.facebook.com) with this we can try Facebook API lively. So how to boost our Facebook Request? This is it…

 

Select Your Field and Limit The Result

 

To increase our facebook request I do recommend you to select field you need, don’t select all the fields or you will wasting your request. And then we need to limit the request, by default Graph API will limit the result just 25 records. But that’s too many (IMO) so I usually limit the result just 10 records per request and that make the request faster 😀 . See the example here.

[code language="ruby"]
	<div title="Page 1">
	 <pre>
	 	FEED_API_FIELDS = "id,from,message,story,type,picture"
	 	LIMIT = 10
	 	graph = Koala::Facebook::API.new(current_user.access_token)
	 	@timeline = graph.get_connections('me', "feed?fields=#{FEED_API_FIELDS}&limit=#{LIMIT}")
	 </pre>
	<div>
[/code]

 

Yeah that’s just a simple request to get current user feed, but remember to limit your field and limit the record response.

 

Use Batch Request

 

You have many request in one page? Like a post, Post a comment, Posting new status, Show my feeds, etc. Then you need Batch Request to do that, we have lot of process but just send one request. See the example below.

[code language="ruby"]
	FEED_API_FIELDS = "id,from,message,story,type,picture"
	LIMIT = 10
	graph = Koala::Facebook::API.new(current_user.access_token)
	@timeline, @newsfeed,
	@liked_pages = graph.batch do |batch_api| batch_api.get_connections('me', "feed?fields=#{FEED_API_FIELDS}&limit=#{LIMIT}") batch_api.get_connections('me', "home?fields=#{FEED_API_FIELDS}&limit=#{LIMIT}") batch_api.get_connections('me', "likes?limit=#{LIMIT}") end
[/code]

 

Yup, as the result we will get 3 data user feed, user home feed and user liked pages in one request. You can see the documentation here.

 

Field Expansion for User Connections

 

If you want to get data which have relations/connections with user, than it’s simpler to use Field Expansion so you can get user data and their connections. Let’s see the code below.

[code language="ruby"]
	FEED_API_FIELDS = "id,from,message,story,type,picture"
	LIMIT = 10
	graph = Koala::Facebook::API.new(current_user.access_token)
	request = "?fields=" #regular user field request += "id,name,hometown,"
	#connection field with user request += "feed.limit(#{LIMIT}).fields(#{FEED_API_FIELDS})," request += "home.limit(#{LIMIT}).fields(#{FEED_API_FIELDS}),"
	request += "likes.limit(#{LIMIT})"
	@response = graph.get_connections('me', request) #split the result to different variable
	@timeline = @response['feed']
	@newsfeed = @response['home']
	@liked_pages = @response['likes']
[/code]

 

We have to split the result before we can use it just like the example above. You can see full list of user connections through this link. You can check the full documentation here.

 

Search

 

Facebook has API to search over all public objects in the social graph with here. There are two params we should pass, first is ‘q’ for keyword and the last is ‘type’ for object type. Check the example :

[code language="ruby"]
	graph = Koala::Facebook::API.new(current_user.access_token) graph.get_connections('search', '?q=aditia+mahdar&type=user')
[/code]

 

But that will search all public object not user related object. You can check about it here. How if we want to search current user friends? How if we want to search current user groups? For this I do recommend you to use FQL than you have to load all data via Graph API and search through its respond. Okay I’ll give you example how to search current user friends through FQL, see the code below :

[code language="ruby"]
	LIMIT = 10
	keyword = 'aditia mahdar'
	graph = Koala::Facebook::API.new(current_user.access_token)
	query = "SELECT uid, name FROM user
		WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
		AND (strpos(lower(name),'#{keyword}')>=0 OR strpos(name,'#{keyword}')>=0)"
	@result = graph.fql_query(query)
[/code]

 

I recommend you to use FQL to do advance query to Facebook API, you can learn about FQL here. Okay I think that’s all what I can share for now, hope this article useful. 🙂

~ Happy Code ~