1. First of all, Register yourself as a developer on facebook.
2. If you are already having a developer account on facebook then create a new app
3. Now download the zip file facebook sdk
4. Now create your project folder give it a name like (“fb”) .
5. now copy the file from the src folder of facebook sdk file and paste it into your project folder(“fb”).
6. Create a file index.html
<!Doctype html>
Through this file, we just show a login button onClick of which we just send it to the connect.php
in which we check the login condition for the facebook
7. There are following three condition for the facebook login user.
i. User is already login on facebook then we directly show him/her permission page.
ii. If user is not login on facbook then we first show him facebook login page then the permission pages.
iii. If user is login but allow certain permission then we show him the rest of the permission.
You can find these three condition in the below code.
8. Create another file connect.php and insert the following code in it.
Connect.php <?php ob_start(); //include the Facebook PHP SDK include_once "facebook.php"; //instantiate the Facebook library with the APP ID and APP SECRET $facebook = new Facebook(array( "appId" => "Your_API_Key", "secret" => "Your_API_Secret_Key", "cookie" => true )); //Get the FB UID of the currently logged in user $user = $facebook->getUser(); //if the user has already allowed the application, you"ll be able to get his/her FB UID if($user) { //start the session if needed if( session_id() ) { } else { session_start(); } //do stuff when already logged in //get the user"s access token $access_token = $facebook->getAccessToken(); //check permissions list $permissions_list = $facebook->api( "/me/permissions", "GET", array( "access_token" => $access_token ) ); //check if the permissions we need have been allowed by the user //if not then redirect them again to facebook"s permissions page $permissions_needed = array("user_photos","user_birthday","user_relationships", "friends_photos","read_stream", "offline_access", "publish_actions"); //these are the permission which we require for the application. foreach($permissions_needed as $perm) { if( !isset($permissions_list["data"][0][$perm]) || $permissions_list["data"][0][$perm] != 1 ) { $login_url_params = array( "scope" => "user_photos,user_relationships,user_birthday,friends_photos,read_stream,offline_access,publish_actions", "fbconnect" => 1, "display" => "page", "next" => "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"] ); $login_url = $facebook->getLoginUrl($login_url_params); header("Location: {$login_url}"); break; exit(); } } //if the user has allowed all the permissions we need, //get the information about the pages that he or she managers $accounts = $facebook->api( "/me", "GET", array( "access_token" => $access_token ) ); //save the information inside the session $_SESSION["access_token"] = $access_token; $_SESSION["accounts"] = $accounts["data"]; //save the first page as the default active page $_SESSION["active"] = $accounts["data"][0]; //redirect to manage.php header("Location: fql.php"); // if the your successfully registered and allowed permission the it move to fql.php file } else { //if not, let"s redirect to the ALLOW page so we can get access //Create a login URL using the Facebook library"s getLoginUrl() method $login_url_params = array( "scope" => "user_photos,user_birthday,user_relationships,friends_photos,read_stream,offline_access,publish_actions", "fbconnect" => 1, "display" => "page", "next" => "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"] ); $login_url = $facebook->getLoginUrl($login_url_params); //redirect to the login URL on facebook header("Location: {$login_url}"); exit(); } ob_flush(); ?>
9. Finally create Fql.php on which you want to access the data suppose (you friends image)
<?php //header("Content-Type: image/png"); require_once("facebook.php"); $facebook = new Facebook(array( "appId" => "Your_API_KEY", "secret" => "Your_API_Secret_key", "cookie" => true, )); $fql1 = "select name,birthday_date,sex,pic_big from user where uid = me();"; $response1 = $facebook->api(array( "method" => "fql.query", "query" =>$fql1, )); print_r($response1[0]["name"]); ?>
9. For