I have been working on facebook apps and connection with Facebook Graph API access tocken. This post will explain you how to integrate facebook connect in your website in easy way to connect and read the Facebook home timeline with PHP. Explained how to get facebook token and user id hope you like it. Thanks !
Make New Application on Facebook:
1. Visit https://developers.facebook.com/apps and click + Create New App.
The script contains two folders called oAuth and images with PHP files.
src
– base_facebook.php // Class Get user Info
– facebook.php // Class Get user Info
– config.php // Configuration file
images
html.inc // html design view
index.php // Main index file show data
PHP Code
Edit config.php
<?php
$config['callback_url'] = 'CALL BACK URL/?fbTrue=true'; // /?fbTrue=true allow you to code process section.
//Facebook configuration
$config['App_ID'] = 'Your App ID';
$config['App_Secret'] = 'Your App Secret';
?>
Index.php
While clicking Login with Facebook button URL requesting Facebook Graph API with contains your web project redirection URL:
$config['callback_url'] = "http://www.yoursite.com/index.php/?fbTrue=true"; // added in config.php
<a href="https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=email,user_likes,publish_stream"><img src="./images/login-button.png" alt="Sign in with Facebook"/></a>
<?php
require 'src/config.php';
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $config['App_ID'],
'secret' => $config['App_Secret'],
'cookie' => true
));
if(isset($_GET['fbTrue']))
{
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=".$config['App_ID']."&redirect_uri=" . urlencode($config['callback_url'])
. "&client_secret=".$config['App_Secret']."&code=" . $_GET['code'];
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
$content = $user;
}
else
{
$content = '<a href="https://www.facebook.com/dialog/oauth?client_id='.$config['App_ID'].'&redirect_uri='.$config['callback_url'].'&scope=email,user_likes,publish_stream"><img src="./images/login-button.png" alt="Sign in with Facebook"/></a>';
}
include('html.inc');