Sunday, September 14, 2014

Creating a facebook login for a website involves the following 3 activities on a high-level. Each of these have been explained in detail below.

  • Obtain App ID and App Secret which is required to initialize the Facebook SDK for login.

  • Download and configure(if required) the Facebook PHP SDK for your website.

  • Authenticate, Authorize and Make requests to the Graph API to obtain the required facebook user information.

Obtaining App ID and App Secret


  • Create and configure a Facebook App in the name of your project here http://developers.facebook.com/apps
  • Click on the Add a New App button as shown below
    Add a New App
  • Select the fourth circle - WWW Website
    Add a New App
  • In the next window, click on "Skip Quickstart" as you don't have anything existing to be configured. Type in the name of your application as desired, select an appropriate category below and click on Create App.
    Create a New App
  • Now you have your App ID and App Secret which you will be making use to initialize the Facebook PHP SDK and get the user information.

    App ID and App Secret

Download the Facebook PHP SDK


You can download the facebook php sdk from here https://github.com/facebook/facebook-php-sdk-v4/archive/4.0-dev.zip

Once downloaded, unpack the contents. Rename the folder "facebook-php-sdk-v4-4.0-dev" to "facebook-php-sdk" for the sake of simplicity. Make sure you move this folder to the same directory which contains your website's index.php i.e., your website's root folder.


Authentication , Authorization and Making requests to the Graph API to obtain the required facebook user information


Facebook login in simple terms can be stated as follows:
  • Create a helper object, which will help you in generating 'Login URL' and 'Logout URL'.
  • Login URL will take a new user to facebook login page for authentication and redirect to a page as specified by redirectUrl of getLoginUrl() on successful authentication.
  • Logout URL will take an signed in user to facebook's logout page and redirect to a page as specified by redirectUrl of getLogoutUrl().
  • Do a bit of session management.
  • Retrieve user information from Facebook's Graph API.
Also note that you will be taken to the page as specified by redirectUrl only after you make an entry of the absolute path name of the redirect page in your facebook's app. This can be done as follows:

Go to Dashboard >> Settings >> Advanced >> Valid OAuth redirect URIs
Make the entry and save changes.

In the code shown below I have considered two files:
  • index.php
  • fbhome.php
The code is run on xampp local host and the project is named as 'floginproj'. index.php contains the login link which when pressed takes you to facebook login page, after authentication you are redirected to 'fbhome.php'. In fbhome.php you can retrieve the necessary user information obtained via the graph API and store them in the database or use them further as required in your website.

Here is the code:

index.php fbhome.php

Common Errors:


In this section, I've listed down the most common errors you might come across while performing the above steps. Most of the errors given below might happen if you are using a php version lesser than php 5.4. Facebook documentation already mentions that. But in case you are using a lower version you might come across these errors and I have provided a fix.

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookResponse.php on line 137

change this >>>
public function getGraphObject($type = 'Facebook\GraphObject') {
return (new GraphObject($this->responseData))->cast($type);
}
to this >>>
public function getGraphObject($type = 'Facebook\GraphObject') {
$GraphObj1=new GraphObject($this->responseData);
$GraphObj=$GraphObj1->cast($type);
return $GraphObj;
}

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookResponse.php on line 154

change this >>>
$out[] = (new GraphObject($data[$i]))->cast($type);
to this >>>
$GraphObjData1=new GraphObject($data[$i]);
$GraphObjData=$GraphObjData1->cast($type);
$out[]=$GraphObjData;

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php on line 146

change this >>>
$response = (new FacebookRequest(
FacebookSession::newAppSession($this->appId, $this->appSecret),
'GET',
'/oauth/access_token',
$params
))->execute()->getResponse();
to this >>>
$response1= (new FacebookRequest(
FacebookSession::newAppSession($this->appId, $this->appSecret),
'GET',
'/oauth/access_token',
$params
));
$response2 = $response1->execute();
$response = $response2->getResponse();

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\GraphObject.php on line 111

change this >>>
return (new GraphObject($value))->cast($type);
to this >>>
$GraphObjProp1 = new GraphObject($value);
$GraphObjProp = $GraphObjProp->cast($type);
return $GraphObjProp;

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\GraphObject.php on line 147

change this >>>
$out[$key] = (new GraphObject($value))->cast($type);
to this >>>
$GraphObjProp1 = new GraphObject($value);
$GraphObjProp = $GraphObjProp->cast($type);
$out[$key] = $GraphObjProp;

Fatal error: Call to undefined function Facebook\session_status() in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php on line 189

change this >>>
protected function storeState($state)
{
if ($this->checkForSessionStatus === true
&& session_status() !== PHP_SESSION_ACTIVE) {
throw new FacebookSDKException(
'Session not active, could not store state.', 720
);
}
$_SESSION[$this->sessionPrefix . 'state'] = $state;
}
to this >>>
protected function storeState($state)
{ $setting = 'session.use_trans_sid';
$current = ini_get($setting);
if (FALSE === $current)
{
throw new UnexpectedValueException(sprintf('Setting %s does not exists.', $setting));
}
$testate = "mix$current$current";
$old = @ini_set($setting, $testate);
$peek = @ini_set($setting, $current);
$result = $peek === $current || $peek === FALSE;
if ($this->checkForSessionStatus === true
&& $result !== true) {
throw new FacebookSDKException(
'Session not active, could not store state.', 720
);
}
$_SESSION[$this->sessionPrefix . 'state'] = $state;
}

Fatal error: Call to undefined function Facebook\session_status() in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php on line 218

change this >>>
protected function loadState()
{
if ($this->checkForSessionStatus === true
&& session_status() !== PHP_SESSION_ACTIVE) {
throw new FacebookSDKException(
'Session not active, could not load state.', 721
);
}
if (isset($_SESSION[$this->sessionPrefix . 'state'])) {
$this->state = $_SESSION[$this->sessionPrefix . 'state'];
return $this->state;
}
return null;
}
to this >>>
protected function loadState()
{ $setting = 'session.use_trans_sid';
$current = ini_get($setting);
if (FALSE === $current)
{
throw new UnexpectedValueException(sprintf('Setting %s does not exists.', $setting));
}
$testate = "mix$current$current";
$old = @ini_set($setting, $testate);
$peek = @ini_set($setting, $current);
$result = $peek === $current || $peek === FALSE;
if ($this->checkForSessionStatus === true
&& $result !== true) {
throw new FacebookSDKException(
'Session not active, could not load state.', 721
);
}
if (isset($_SESSION[$this->sessionPrefix . 'state'])) {
$this->state = $_SESSION[$this->sessionPrefix . 'state'];
return $this->state;
}
return null;
}


Fatal error: Uncaught exception 'Facebook\FacebookSDKException' with message 'Session not active, could not load state.' in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php:228 Stack trace: #0 C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php(132): Facebook\FacebookRedirectLoginHelper->loadState() #1 C:\xampp\htdocs\floginproj\fbhome.php(22): Facebook\FacebookRedirectLoginHelper->getSessionFromRedirect() #2 {main} thrown in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php on line 228



Catchable fatal error: Method Facebook\Entities\AccessToken::__toString() must return a string value in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookSession.php on line 85

Parse error: syntax error, unexpected '[' in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\HttpClients\FacebookStreamHttpClient.php on line 143

change this >>>
public function compileHeader()
{
$header = [];
foreach($this->requestHeaders as $k => $v) {
$header[] = $k . ': ' . $v;
}
to this >>>
public function compileHeader()
{
//$header = [];
foreach($this->requestHeaders as $k => $v) {
$header[] = $k . ': ' . $v;
}



Fatal error: Uncaught exception 'Facebook\FacebookSDKException' with message 'You must provide or set a default application secret.' in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookSession.php:340 Stack trace: #0 C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRequest.php(283): Facebook\FacebookSession::_getTargetAppSecret() #1 C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRequest.php(204): Facebook\FacebookRequest->getAppSecretProof('149425558081437...') #2 C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php(146): Facebook\FacebookRequest->__construct(Object(Facebook\FacebookSession), 'GET', '/oauth/access_t...', Array) #3 C:\xampp\htdocs\floginproj\fbhome.php(32): Facebook\FacebookRedirectLoginHelper->getSessionFromRedirect() #4 {main} thrown in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookSession.php on line 340

include this line
FacebookSession::setDefaultApplication( 'your App ID','your App Secret' );



Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\floginproj\fbhome.php on line 38

change this >>>
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
to this >>>
$request = (new FacebookRequest($session, 'GET', '/me' ));
$response = $request->execute();
$user_profile = $response->getGraphObject(GraphUser::className());

Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message 'This authorization code has been used.' in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRequestException.php:104 Stack trace: #0 C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRequest.php(268): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 400) #1 C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRedirectLoginHelper.php(147): Facebook\FacebookRequest->execute() #2 C:\xampp\htdocs\floginproj\fbhome.php(32): Facebook\FacebookRedirectLoginHelper->getSessionFromRedirect() #3 {main} thrown in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\FacebookRequestException.php on line 104

[Needs session management please read the above tutorial and implement as suggested]

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\GraphSessionInfo.php on line 64

change this >>>
return (new \DateTime())->setTimestamp($stamp);
to this >>>
$DateTimeObj = new \DateTime();
return $DateTimeObj->setTimestamp($stamp);

9 comments:

  1. Awesome, Really helps a lot, thanks :)

    ReplyDelete
  2. Hi, this tutorial are awesome!
    But i still have a problem:
    I have correct all the errors, but i have one more on

    Parse error: syntax error, unexpected T_PUBLIC in .../src/Facebook/HttpClients/FacebookStreamHttpClient.php on line 156

    In this part....

    public static function formatHeadersToArray(array $rawHeaders)
    {
    $headers = array();

    foreach ($rawHeaders as $line) {
    if (strpos($line, ':') === false) {
    $headers['http_code'] = $line;
    } else {
    list ($key, $value) = explode(': ', $line);
    $headers[$key] = $value;
    }
    }

    return $headers;
    }


    I hope you can help me...
    Many thanks

    ReplyDelete
    Replies
    1. Hi Vale, Can you tell me which line in particular?

      Delete
  3. How to solve the following ERROR:
    Fatal error: Call to undefined method Facebook\FacebookSession::getProperty()
    For the following CODE:

    $request = new FacebookRequest(
    $session,
    'GET',
    '/me/albums'
    );
    $response = $request->execute();
    $graphObject = $response->getGraphObject();

    $album = $request->getProperty('data');

    $album_data = $album->asArray();//this will do

    all job for you..
    sizeof($album_data);


    Please Help me soon

    ReplyDelete
    Replies
    1. add the following line:

      use Facebook\FacebookSession;

      Delete
  4. hi im getting a error plz help...

    HTTP Status 500 - java.lang.RuntimeException: PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\Users\HP USER\Downloads\Apache Tomcat 7.0.35\apache-tomcat-7.0.35\webapps\JavaBridge\fb\lib\Facebook\GraphSessionInfo.php on line 90

    java.lang.RuntimeException: PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR in C:\Users\HP USER\Downloads\Apache Tomcat 7.0.35\apache-tomcat-7.0.35\webapps\JavaBridge\fb\lib\Facebook\GraphSessionInfo.php on line 90

    in this part...

    public function getIssuedAt()
    {
    $stamp = $this->getProperty('issued_at');
    if ($stamp) {
    return (new \DateTime())->setTimestamp($stamp);
    } else {
    return null;
    }
    }

    ReplyDelete
  5. Am getting below error::
    Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in C:\xampp\htdocs\floginproj\facebook-php-sdk\src\Facebook\HttpClients\FacebookStream.php on line 74

    ReplyDelete
  6. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Take me to another useless website

    ReplyDelete