
Facebook Login API Integration in your Website
- October 7, 2015
- Leave a comment
Nowadays, a large number of websites provide a way of login with Facebook to its visitors. This post will explain you how to integrate Facebook Login API in your website in an easy way to connect and read Facebook home timeline with PHP. You need to follow the steps given below:
1- Create Application on Facebook:
Visit developers.facebook.com and click on “Create New App“.
2- Connection Established with Facebook:
Establish a connection with Facebook by running the following code:
1 2 3 4 5 6 |
<?php $config['callback_url'] = 'CALL BACK URL/?fbTrue=true'; ////?fbTrue=true allow you to code process section. $config['callback_url'] = "http://www.yoursite.com/index.php/?fbTrue=true"; $config['App_ID'] = 'Your App ID'; $config['App_Secret'] = 'Your App Secret'; ?> |
3- Facebook Login Button:
Create Facebook Login button by using the following code:
1 |
<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> |
4- Facebook Login Api:
Final step is to integrate the Facebook Login API in your website. For this, run the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
require 'src/config.php'; require 'src/facebook.php'; // Create our Application $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'); |
Conclusion:
A large number of plugins are available on internet for integrating user login with Facebook. But the disadvantage is that every plugin have some extra features which might not be needed. It might also cause your website to slow down. This is just a simple and easy way to connect your website with Facebook Login API.
User Comments