Quantcast
Channel: Thinkdiff.net » connect
Viewing all articles
Browse latest Browse all 2

PHP SDK 3.0 & Graph API base Facebook Connect Tutorial

$
0
0

facebookFacebook recently updated their PHP-SDK to version 3.0 also added new way of authentication. This is a major change. So this is the updated post of my old popular post.

In this version of my code, I avoided javascript authentication, because that is cookie based and php sdk 3.0 has some conflicts with the old system. Facebook will also update the javascript sdk soon, so later you can use the login/logout system of javascript api with php sdk. But for now you should use fully php base authentication.

In this post you’ll learn:

  1. How to integrate Facebook Connect in your website
  2. How to generate Facebook Login / Logout URL
  3. How to get extended permissions from users
  4. How to call graph api
  5. How to run FQL Query
  6. Publish Wall Post using PHP
  7. Publish Wall Post using Facebook Javascript API
  8. Invite friends using Facebook Javascript API
  9. No Facebook Javascript only use PHP SDK

Before proceeding:

Demo App

For iFrame Base Facebook app checkout this tutorial

1. How to integrate Facebook Connect in your website

You have to create a facebook app and provide callback url. Checkout some screenshots of this article. If you unzip my downloaded code you’ll see these files (fig) where fbmain.php and index.php is coded by me. Then just copy the appid and secret key and paste them in the fbmain.php . I put all authentication and graph api code in fbmain.php and index.php is used for showing them, but remember index.php is the main file that you mention in your callback url . And this file includes fbmain.php at top.

//facebook application
    $fbconfig['appid' ]     = "";
    $fbconfig['secret']     = "";
    $fbconfig['baseurl']    = "";// "http://thinkdiff.net/demo/newfbconnect1/php/sdk3/index.php";

2. How to generate Facebook Login / Logout URL

In fbmain.php you’ll see

$loginUrl   = $facebook->getLoginUrl(
            array(
                'scope'         => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
                'redirect_uri'  => $fbconfig['baseurl']
            )
    );

    $logoutUrl  = $facebook->getLogoutUrl();

and in template.php you’ll see this code. This code shows user the login and logout url.

 <?php if (!$user) { ?>
        You've to login using FB Login Button to see api calling result.
        <a href="<?=$loginUrl?>">Facebook Login</a>
    <?php } else { ?>
        <a href="<?=$logoutUrl?>">Facebook Logout</a>
    <?php } ?>

To learn about authentication and $user please follow this tutorial

3. How to get extended permissions from users

You’ve to provide the extended permission at the time when you generate login url. And this is ‘scope’ where you’ll provide the extended permissions as a comma separated list.

$loginUrl   = $facebook->getLoginUrl(
            array(
                'scope'         => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
                'redirect_uri'  => $fbconfig['baseurl']
            )
    );

To know more about extended permissions visit facebook official documentation

4. How to call graph api

Its very simple. You’ll see below code in fbmain.php

       //get user basic description using graph api
        $userInfo           = $facebook->api("/$user");

        //Retriving movies those are user like using graph api
        try{
            $movies = $facebook->api("/$user/movies");
        }
        catch(Exception $o){
            d($o);
        }

To know more about graph api visit facebook official documentation

5. How to run FQL Query

In fbmain.php you’ll see below code

try{
            $fql    =   "select name, hometown_location, sex, pic_square from user where uid=" . $user;
            $param  =   array(
                'method'    => 'fql.query',
                'query'     => $fql,
                'callback'  => ''
            );
            $fqlResult   =   $facebook->api($param);
        }
        catch(Exception $o){
            d($o);
        }

To know more about FQL checkout facebook official documentation

6. Publish Wall Post using PHP

In fbmain.php you’ll see below code, if $_GET['publish'] is set then using php sdk and graph api the post will publish in user’s profile. You can write this code in separate php file and call via ajax to publish, in my example I use normal way to publish with no ajax. In the demo you’ll see Publish Post using PHP so click this link to publish in your wall.

if (isset($_GET['publish'])){
            try {
                $publishStream = $facebook->api("/$user/feed", 'post', array(
                    'message' => "I love thinkdiff.net for facebook app development tutorials. :) ",
                    'link'    => 'http://ithinkdiff.net',
                    'picture' => 'http://thinkdiff.net/ithinkdiff.png',
                    'name'    => 'iOS Apps & Games',
                    'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
                    )
                );
                //as $_GET['publish'] is set so remove it by redirecting user to the base url
            } catch (FacebookApiException $e) {
                d($e);
            }
        }

7. Publish Wall Post using Facebook Javascript API

In index.php you’ll see a javascript function

function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){
                FB.ui({ method : 'feed',
                        message: userPrompt,
                        link   :  hrefLink,
                        caption:  hrefTitle,
                        picture: 'http://thinkdiff.net/ithinkdiff.png'
               });
               //http://developers.facebook.com/docs/reference/dialogs/feed/

            }

If you call this method, you’ll see a popup windows, that will prompt you to publish in your wall. In this case you don’t need any php.

8. Invite friends using Facebook Javascript API

Same as above in index.php you’ll see a javascript function. Just call this function to see a Request dialog.

function newInvite(){
                 var receiverUserIds = FB.ui({
                        method : 'apprequests',
                        message: 'Come on man checkout my applications. visit http://ithinkdiff.net',
                 },
                 function(receiverUserIds) {
                          console.log("IDS : " + receiverUserIds.request_ids);
                        }
                 );
                 //http://developers.facebook.com/docs/reference/dialogs/requests/
            }

9. No Facebook Javascript only use PHP SDK

If you don’t want to use any facebook javascript api, then remove all the javascript code from index.php and also remove below part

<div id="fb-root"></div>
    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
     <script type="text/javascript">
       FB.init({
         appId  : '<?=$fbconfig['appid']?>',
         status : true, // check login status
         cookie : true, // enable cookies to allow the server to access the session
         xfbml  : true  // parse XFBML
       });

     </script>

If you remove the above part facebook javascript will not work.

Hope this tutorial will help you to understand PHP SDK 3.0 and its usage properly.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images