Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create an application using Facebook 4.0 SDK

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 479
    Comment on it

    To create a new Facebook application, register on https://developers.facebook.com and make a new application that will generate a Facebook application id and secret key which you will use in your development.

    After creating the application open the settings tab, in that click on add platform. You can make your application on facebook by selecting the 'Application on Facebook' or you can select 'Website'.

    You can download the sdk from this link: https://developers.facebook.com/docs/php/gettingstarted/4.0.0 and then use composer for downloading the dependency. When all the files are downloaded you will get a vendor folder. You will start your code by including the autoload.php file.

    if (($loader = require_once __DIR__ . '/vendor/autoload.php') == null)  {
          die('Vendor directory not found, Please run composer install.');
        }   
        use Facebook\FacebookSession;
        use Facebook\FacebookRequest;
        use Facebook\GraphUser;
        use Facebook\GraphObject;
        use Facebook\FacebookRequestException;
        use Facebook\FacebookJavaScriptLoginHelper;
        use Facebook\FacebookRedirectLoginHelper;
        session_start();
    

    If you are using Facebook api for website then use this code:

    use Facebook\FacebookRedirectLoginHelper; 

    For Facebook canvas application use this code:
    use Facebook\FacebookCanvasLoginHelper;

    Initialize your facebook api and get session like this

    $app_url="URL TO index.php file";
    FacebookSession::setDefaultApplication('','');
    $helper = new FacebookRedirectLoginHelper($app_url); //
    try {
            $session = $helper->getSessionFromRedirect(); //Will get a seession id from redirect
            $_SESSION['session']= serialize($session); //Storing session variable into session.       
        } catch(FacebookRequestException $ex) {
            /*echo "
    FacebookRequestException:"; echo $ex->getMessage();*/ } catch(\Exception $ex) { /*echo "
    Exception:"; echo $ex->getMessage(); $user = null;*/ } if($session) { try { $graphObject = (new FacebookRequest( $session, 'GET', '/me' ))->execute()->getGraphObject()->asArray(); /*Getting User information my GET request if we have Session variable*/ /* You can store the User info into database and then redirect them to other page */ } catch(FacebookRequestException $e) { echo "Exception occurred, code: " . $e->getCode(); echo " with message: " . $e->getMessage(); } }

    If you do not have a session variable then use getLoginUrl and in that you can pass the scope array.
    Scope are the permission which you ask from users so that you can get user related information from Facebook.

     else {
      echo '< script>';
    echo 'window.location.href="'.$helper->getLoginUrl( array('user_location,email,user_birthday,user_photos') ).'";';           /* Authorizing users with scope*/
    echo '< /script>';
      } 
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: