How to Login with Facebook API SDK v5 in PHP
Previously we wrote many article on Facebook login and now they are not working due to updates in Facebook API so I am writing on latest SDK v5 how to login with Facebook using PHP SDK v5 which is the latest version and I suggest you all to update your old Facebook login integration to the latest version.
Let’s Start
Make New Application on Facebook:
1. Visit https://developers.facebook.com/apps and click + Create New App.
Click on website.
Add application name and click Create New Facebook App ID.
Select category and click on Create App ID. On next window click Skip Quick Start.
It will take you to the app dashboard now select settings and click on +Add Platform. Select website in add platform popup.
Add your website link and fill form and save like below image.
By default your app enabled for sandbox mode you need to change it to live click on Status & Review.
That’s it App settings done now come to the coding.
First of all download SDK from here or you can download our demo code which contain SDK as well.
Now create a file index.php and add a facebook login button link to fblogin.php page and create a callback.php page to show information after authenticate.
index.php
<!DOCTYPE html> <html> <head> <title>Login With Facebook</title> </head> <body> <a href="fblogin.php"><img src="login-button.png" alt="Facebook Login Button"></a> </body> </html>
It will show a login with Facebook image.
fblogin.php
<?php session_start(); require_once( 'Facebook/autoload.php' ); $fb = new Facebook\Facebook([ 'app_id' => 'ENTER-YOUR-APP-ID', 'app_secret' => 'ENTER-YOUR-APP-SECRET', 'default_graph_version' => 'v2.5', ]); $helper = $fb->getRedirectLoginHelper(); $permissions = ['email']; // Optional permissions for more permission you need to send your application for review $loginUrl = $helper->getLoginUrl('http://demo.phpgang.com/facebook-login-sdk-v5/callback.php', $permissions); header("location: ".$loginUrl); ?>
This page verify your Facebook application credentials and redirect you to facebook.com for login. You need to change your application id and application secret.
Once you allow all permission it will redirect to callback.php page and show your user’s data.
callback.php
<?php session_start(); require_once( 'Facebook/autoload.php' ); require_once( 'db.php' ); $fb = new Facebook\Facebook([ 'app_id' => 'ENTER-YOUR-APP-ID', 'app_secret' => 'ENTER-YOUR-APP-SECRET', 'default_graph_version' => 'v2.5', ]); $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; } try { // Get the Facebook\GraphNodes\GraphUser object for the current user. // If you provided a 'default_access_token', the '{access-token}' is optional. $response = $fb->get('/me?fields=id,name,email,first_name,last_name', $accessToken->getValue()); // print_r($response); } catch(Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error echo 'ERROR: Graph ' . $e->getMessage(); exit; } catch(Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues echo 'ERROR: validation fails ' . $e->getMessage(); exit; } $me = $response->getGraphUser(); //print_r($me); echo "Full Name: ".$me->getProperty('name')."<br>"; echo "First Name: ".$me->getProperty('first_name')."<br>"; echo "Last Name: ".$me->getProperty('last_name')."<br>"; echo "Email: ".$me->getProperty('email'); echo "Facebook ID: <a href='https://www.facebook.com/".$me->getProperty('id')."' target='_blank'>".$me->getProperty('id')."</a>"; ?>
That’s it for codding end now you can integrate it and run latest version of Facebook login.
Note: This version of the Facebook SDK for PHP requires PHP 5.5 or greater.
Tutorial Categories:
hello my dear .. this not working ..error message.. Invalid App ID: {455718634633119}
please help me
please remove { } brackets not required.
It is giving….
Facebook SDK returned an error: Connection timed out after 10001 milliseconds
share link here please.
Thankx , finally i solved the error .
facing same error
how???pls help me
Please how did you solve this??
hello, its not working. error message Warning: Unexpected character in input: ” (ASCII=92) state=1 and Parse error: syntax error, unexpected T_STRING in
plz help
Its in your php code error find that slash and remove it.
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Its not my php code error. Error in fblogin.php page on line no 5.
Plz help
Hello, everytime i am getting error “Facebook SDK returned an error: Cross-site request forgery validation failed. Required param “state” missing.” but State is available in the get URL parameters while callback.
@huzoorbux:disqus
Can you please suggest some solution?
please share code.
@huzoorbux:disqus … Code is exactly same as written here in tutorial. Not even one line is changed.
Just downloaded code and execute it its working perfect..
is it something to do with my App settings then ?? i have two links on same page to register and login and both are pointing to login facebook file.. Do you think that can create issues?
only follow instruction given in above tutorial
Followed same and one thing more that i am using without `.php` extension.. Will that be a problem ?? and how can i share the code with you..
hi, i use this php with modifications like change to v2.7 and have same problem,
i can log 20 30 times with no problem but sometimes show this:
Facebook SDK returned an error: Cross-site request forgery validation failed. The “state” param from the URL and session do not match.
on same pc, and user i try 30 log and logout: 29 ok 1 this problem
good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?
Facebook has retracted these features of the API due to privacy concerns.
Source: http://www.guardian.co.uk/technology/2011/jan/18/facebook-privacy-data
good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?
good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?
good Work thanks…. i have a question? can i ask permission and get account phone number ? or phone number it’s only for page informations?
@huzoorbux:disqus
I am getting
Call to a member function getValue() on a non-object on $response = $fb->get(‘/me?fields=id,name,email,first_name,last_name’, $accessToken->getValue());
@huzoorbux:disqus
Downloaded file not working. I haven’t replaced anything.
Facebook SDK returned an error: couldn’t connect to host
Your server probably blocks outgoing CURL requests, contact your hosting provider.
Great work i downloaded That its work perfect Thanks …….
It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome
It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome
It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome
It is working perfectly. I can bet that there can’t be such beautiful tutorial any where else. Really it is awesome
Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in D:wampwwwDemosfacebookLoginDemofblogin.php on line 4
Facebook SDK returned an error: Connection timed out after 10001 milliseconds
Share your website link.
nice tutorial
nice!
help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5
This error appears because your PHP version is lower than 5.4. Facebook config array is written with square brackets. If you can’t upgrade PHP version solution I used is to replace “[xxxxx => yyyy]” array definition with “array(xxxx=>yyyy)” in your files as well as Facebook PHP SDK files.
Facebook SDK 5.0 requires min PHP 5.4
help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5
help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5
help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5
help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5
help please! Parse error: syntax error, unexpected ‘[‘, expecting ‘)’ in C:Amppswwwfbfblogin.php on line 5
Is it possible to capture my posts and hometown/location ?
If I increment the code as:
get(‘/me?fields=id,name,email,first_name,last_name,posts,location,hometown’)
and :
echo “Posts: “.$me->getProperty(‘posts’).””;
Is it possible? Because I increment this and it did not work.
Thanks!!
It will not work like that you need to follow this forum: http://stackoverflow.com/questions/31390197/facebook-graph-api-me-feed-v2-4-vs-v2-3
Facebook SDK returned an error: Connection timed out after 10001 milliseconds
Make sure that you are using php 5.5 or greater.
link -http://haridwaryatra.com/fb/
Getting Error – Facebook SDK returned an error: Connection timed out after 10001 milliseconds
Hello
Hope you are doing great
we are using facebook APi when i have click facebook login then redirect to my site its display error message “An Internal Error Has Occurred
Error: An Internal Error Has Occurred.” and not $accessToken = $helper->getAccessToken(); print access token
please let us know how can do it in cakephp 3.1
Hello sir,
This bunch of code is not working it will show a error -> ” Facebook SDK returned an error: Connection timed out after 10001 milliseconds
Run this code on your server
<?php
echo file_get_contents(“https://graph.facebook.com”);
?>
Show me output.
Sir
this is the error that i am getting…
Your hosting block graph.facebook.com contact hosting provider.
sir this the eroor tht i have getting
And sir I am sure about my Php version is grater than 5.5..
Run this code on your server
Show me outpu6t.
sir I am running this codes in my server.but i got the same error…pls help me
Your hosting block graph.facebook.com contact hosting provider.
Sir I will send screen shot of error..pls See
OK sir
How to get Profile url (Ex. fb.com/your_profile_user_name)
when I run this script code I got this error “Warning: Unexpected character in input: ” (ASCII=92) state=1…line no. 5″ there is “$fb = new FacebookFacebook([” where I mistake??
also same line second error this
“Parse error: syntax error, unexpected T_STRING” line no. 5
What PHP version you have installed?
PHP Version 5.2.17
PHP 5.5 or greater reqired.
is there any thing which I can do in 5.2.17 version
No new api sdk require phpp 5.6 minimum
Thanks for the great post.
I am stuck as some issue here about how to pass the appsecret_proof param to the fb request. I have added a question with complete details here as well – http://stackoverflow.com/questions/40500252/passing-appsecret-proof-in-fb-request
Can someone help me figure it out. Thanks.
Thanks for the great post. Have you developed code to post message, image on FB page using V5 sdk? If yes please share
Thanks for the great post. Have you developed code to post message, image on FB page using V5 sdk? If yes please share
Thanks for the great post. Have you developed code to post message, image on FB page using V5 sdk? If yes please share
Thanks that’s great, FB doco’s are not good!
Hello Sir,
I wanna make a image gallery by using Facebook uploaded pictures.Sir would u like to help me out? please
Nice tutorial
FB updated its API version, now its not working, please help
Fb updated their API version and now my login with facebook functionality not working, please help me
i go this:
Facebook SDK returned an error: Connection timed out after 10001 milliseconds
Thank you very much. This is one the best tutorial ever on the subject
Can you please add how to log the member out ?