Facebook connect tutorial and api call


facebookThis article is now deprecated because facebook changed their connect system. So have a look my updated post http://thinkdiff.net/facebook/graph-api-javascript-base-facebook-connect-tutorial/

There is nothing for me to write about facebook connect tutorial. Because if you visit Facebook_Connect_Tutorial1 You’ll see nicely written tutorial so that you could easily integrate facebook connect for your site. But in this article I will add little information for facebook connect.

I assume that you read the following link and already know implementation of facebook connect. I also assume you know how to setup a facebook canvas application. For learning canvas application setup you should follow this tutorial Creating_a_Platform_Application

Q1. User is connected your site by facebook connect. Now how could I detect user is logged in or not?


Answer:

PHP Solution:

$facebook       =   new Facebook($config['fb']['api_key'], $config['fb']['secret_key']);
$user           =   $facebook->api_client->user;
$session        =   $facebook->api_client->session_key;

if (!empty($user) && !empty($session)){
   echo 'user is connected';
}
else{
  echo 'user is not connected';
}

Javascript Solution:

if ( FB.Facebook.apiClient.get_session()){
   var user = FB.Connect.get_loggedInUser();
   // if here comes that means user is connected by fbconnect
}
else{
   // user is not connected or logged in by facebook
}

Q2. User is connected & I want to call api to get some information. How can I do so ?
Answer:

PHP Solution:

$facebook     =   new Facebook($config['fb']['api_key'], $config['fb']['secret_key']);
$user         =   $facebook->api_client->user;
$session      =   $facebook->api_client->session_key;

if (!empty($user) && !empty($session)) {
    $query         =   "SELECT name from user where uid=" $user;
    $result        =   $facebook->api_client->fql_query($query); //api call
}

Javascript Solution:

if ( FB.Facebook.apiClient.get_session()){
    var user = FB.Connect.get_loggedInUser();
    FB.Facebook.apiClient.fql_query("SELECT name FROM user WHERE uid=" + user,
                  function(rows) {
                      var data = "name=" + rows[0].name;
                      console.log(data);
                  }
    );
}

So you can use php or javascript to check authentication or calling api from your facebook connect base site by this way. Very easy, right ? 🙂

mahmud ahsan

Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

You may also like

2 Comments

  • ranacse05
    April 8, 2010 at 4:35 pm

    nice tutorial , thanks .

  • Salman
    January 27, 2011 at 3:40 am

    how can i get user session and its information when our api allow him to connect and also store his session on my localhost i make this api for testing for personal use(localhost internet working all the time)

Comments are closed here.

About Me

Hi I am Mahmud Ahsan. I Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

Twitter Feed

Fanpage