how to create login with facebook button using php


I am creating a web application using PHP and I would like to add login with facebook button in it's login page , how can i do it?

 


Asked by:- pika
2
: 208 At:- 5/22/2017 7:00:55 AM
php facebook login






1 Answers
profileImage Answered by:- vikas_jk

Ok so I am not a very good PHP developer, but here is possible solution

1 » Goto https://developers.facebook.com/apps/ and Click Add a New App.
» Choose Website
» Choose Name for you App and Click Create New Facebook App ID
» Choose a category for you App and click Create App ID
» Now Click Skip Quick Test

2. Install Facebook SDK to Your PHP Project

Now that your “Facebook app” is set up, it’s time to start coding. Facebook has made implementation very simple by providing an SDK for handling most of the heavy lifting. Installation instructions can be found at https://developers.facebook.com/docs/php/gettingstarted.

3. Create a login page

$fb = new Facebook\Facebook([
  'app_id' => '{app-id}', // Replace {app-id} with your app id
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.2',
  ]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('https://example.com/fb-callback.php', $permissions);

echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';

4. Create Callback Page

The callback page is where your website will ensure that the user is logged in to Facebook and has authenticated your website. If so, then you can authenticate them into your website.

// Ensure that sessions are started by this point using session_start()

<?php
$fb = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.2',
]);
 
$helper = $fb->getRedirectLoginHelper();
 
try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
 
if (! isset($accessToken)) {
  if ($helper->getError()) {
    header('HTTP/1.0 401 Unauthorized');
    echo "Error: " . $helper->getError() . "\n";
    echo "Error Code: " . $helper->getErrorCode() . "\n";
    echo "Error Reason: " . $helper->getErrorReason() . "\n";
    echo "Error Description: " . $helper->getErrorDescription() . "\n";
  } else {
    header('HTTP/1.0 400 Bad Request');
    echo 'Bad request';
  }
  exit;
}
 
// Logged in
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
 
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
 
// Get user’s Facebook ID
$userId = $tokenMetadata->getField('user_id');
?>

5. Pull Personal Information from Facebook

When registering a user to your website, it is often useful to store personal information into your database, such as the user’s name. The following code shows how to get a person’s name and user ID from the Facebook API. This example uses the $accessToken that was set in the code example in the previous section.

<?php
try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', $accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
 
$user = $response->getGraphUser();
 
$userId = $user['id']; // Retrieve user Id
$userName = $user['name']; // Retrieve user name
?>

The last two variables will contain the user ID and username. To pull additional information about the user, add the type to the API call (/me?fields=id,name) and then read the value(s) from the $user variable.

Another method 
go to this URL

2
At:- 5/22/2017 7:24:35 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use
Amazon Deals

Jobs from Indeed