In this tutorial we will show how to login with facebook using PHP and facebook Graph API. In this way you can integrate facebook in your website or you can also create a web app using this method.
You may also like login with google using PHP.
To Login With Facebook Using PHP It Takes Only Two Steps:-
- Create a facebook app and get all the details
- Make a PHP file to login with facebook
Step 1. Create a facebook app and get all the details
Create a facebook app here https://developers.facebook.com/appsthen get your facebook app id and facebook app secret and then edit configure.php file and insert your details and callback url in respective variable you will find that file in src folder.
You may also like login with twitter using PHP.
Step 2. Make a PHP file to login with facebook
We make a PHP file and save it with a name fb_login.php
<?php require 'src/config.php'; require 'src/facebook.php'; $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'); ?> <html> <body> <?php $config['callback_url'] = "http://www.your_site.com/index.php/?fbTrue=true"; ?> <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> </body> </html>
In this step we add all the required files for facebook login and then create a facebook array
and insert all the required variables in facebook graph url provided by facebook and finally get the user details.
You may also like login with linkedin using PHP.
Thats all, this is how to do login with facebook using PHP. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
I hope this tutorial on login with facebook helps you and the steps and method mentioned above are easy to follow and implement.