samedi 28 mai 2011

Application Authentication with Facebook PHP SDK

Welcome,

In this first tutorial, I'm gonna explain the application authentication process under Facebook PHP SDK.

1- You have to download the latest Facebook SDK 3.0.0
Github link : https://github.com/facebook/php-sdk/

2- Now you have to include "facebook.php".

3- To verify that the user is logged-in, you can use getUser() function, if the access token is valid, and user is logged-in, it will return the user ID, else we have to redirect the user to the application install page.

Example :
<?php
require 'facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
// We have a valid access token, so we can retrieve user basic informations "ID, name, sex etc....."
  $me = $facebook->api('/me');
  echo "Hello my name is : {$me['name']}";
} else {
// We have to user getLoginUrl() to generate Application install URL, and redirect user via PHP header function, or Javascript.
  $loginUrl = $facebook->getLoginUrl();
  echo "<script>top.location.href=\"{$loginUrl}\"</script>";
}
 
 
That's All,
Feel free to post any question.
Mahdi Talhi.

Aucun commentaire:

Enregistrer un commentaire

Many Ways of Making Money Online

You can learn quite a bit about making money online if you have good information first. Here you’re going to learn what goes into th...