Jump to content

[Tutorial] PHP & Facebook Graph API


Recommended Posts

Hello,

 

I didn't know where to put this so I'll add it here in the hope of it getting move to the tutorials page on the homepage.

 

Getting Started

What do I need?

[*]A server with PHP - Here

[*]The latest version of PHP - Here

[*]Developers Application on Facebook - Here

 

I've got the files now what?

We need to make our config.php file

$app_id = '***************'; // Get this from facebook.com/developers/
$application_secret = '******************************'; // Same as above
$facebook = new Facebook(array( // This will used to call all of Facebook's API
  'appId'  => $app_id,
  'secret' => $application_secret,
  'cookie' => true // This allows cookies to be used. Note: Problems with IE6 and below. Ill talk about it later.
));
$session = $facebook->getSession();// Recover a session if there is one

 

Now we have made a connection to Facebook. We just need to do some checks;

try { // We use try and catch because it is the easy way to do things. Always use try and catch when doing a Facebook API request
        $uid = $facebook->getUser(); // This will throw an error if they are not logged in
        $me = get_user_info('me'); // This will throw an error if they dont allow this application
$friends = $facebook ->api('/me/friends'); // Now we know they are logged in we can get there information like friends
$useFacebook=true; // Now do what you want. You got all there details held in $me array
}catch(FacebookApiException $e){ // O no, we got an error
$url = $facebook->getLoginUrl(array( // This will generate a URL to point the user to allow our application
               'canvas' => 1, // I don't no why but yes
               'fbconnect' => 0, // I don't no why but yes otherwise you get a weird redirect loop
               'req_perms' => 'publish_stream' // Now this is where you can get the user to allow your application to do stuff like posting 
           ));
$useFacebook=false; // Do what you want but remember there not logged in so you wont be able to get all there details
}

 

And that is mostly it. If you want to force them to loggin put this before

$useFacebook=false;

and put

echo '<script type="text/javascript">top.location.href = "'.$url.'";</script>';die;exit;

This will redirect the user when he gets to your page straight away.

 

All in one now;

$app_id = 'id';
$application_secret = 'pass';
$facebook = new Facebook(array(
  'appId'  => $app_id,
  'secret' => $application_secret,
  'cookie' => true
));
$accessToken = $facebook->getAccessToken();
$session = $facebook->getSession();
    try {
        $uid = $facebook->getUser();
        $me = get_user_info('me');
$friends = $facebook ->api('/me/friends');
}catch (FacebookApiException $e){
$url = $facebook->getLoginUrl(array(
               'canvas' => 1,
               'fbconnect' => 0,
               'req_perms' => 'publish_stream'
           ));
echo '<script type="text/javascript">top.location.href = "'.$url.'";</script>';die;exit;
}

 

Now just have to read the Facebook Terms of Service.

I had an application on Facebook that got taken down because it broke one rule. They didn't put it back up so I had to recreate the game then put it back under a different name. The application got 4000 plays an hour but since it's been put back up its not getting that much.

Clicky Click Game

It gave me an important lesson. Always read the Terms & Conditions.

 

Well, I hope I helped you get started.

I almost forgot about that issue with IE6 and below. IE6 blocks the cookies for some reason but you can allow them by having a P3P header or by telling your user to change the Privacy settings to low or Off.

 

And thats about it. If you ever get stuck go to the Facebook Developers site and look there. If that fails there is always trusty PHPFreaks here.

 

Thank-you for reading

Paul

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.