login with facebook php sdk – a tutorial
Publicado; 17/05/2011 Arquivado em: php, webdev | Tags: api, facebook, graph, login, php, webservices 28 ComentáriosI’ve been testing the facebook php-sdk for a while and I had some problems with it. mostly, I had some cookies issues, not being set correctly, or simply not working. I had solved these problems and I’ll show you how to do it.
my system is a fedora 14 64 bits and I’m behind an apache 2.2.17 with php 5.3.6. also, I’m using the facebook php-sdk 2.1.2, available at github. the facebook-php-sdk requires the extension php-curl to be installed.
creating an facebook app
first of all, you’ll need to create an application inside facebook. it’s very easy, follow these steps:
1. go to http://developers.facebook.com/
2. click on “My Apps”, in the near top right corner
3. click on “Set Up New App”
4. set a name of the app, read the docs, select “I agree” and click on “Create App”
5. you’ll need to confirm a captcha and we’re done.
if everything runs fine, you’re now on your new application edit page. if you’re not, I’ll show you how to find it:
1. go to http://www.facebook.com/developers/
2. click on “See My Apps” in the right column
3. select you application in the left column and click on “edit configurations”
currently, we’ll only to need to configure a little. select “Web Site”, in the left column and:
1. copy the “Application ID” and the “Application Secret”. You’ll need them to access facebook.
2. in “Site URL”, I put “http://localhost/fb/”, since I’m just doing tests.
3. in “Site Domain”, I put “localhost”, since I’m just doing tests.
you’ll need to update these info if you’re in a live website. and, of course, there are a lot of other relevant info you’ll may want to update as well.
loging in with facebook
what I’m going to do here is just an very very basic example. if you want to consume relevant user info or do other advanced tasks, you’ll have to refer to the official docs. this is just a pre-step so you can run the sdk correctly.
we’re going localhost. its fairly easy:
1. create a folder in your apache document folder folder called ‘fb’ (depending on your system, it may be called ‘htdocs’, ‘/var/www/’, ‘/var/www/html/’ or something like that)
2. open the facebook-php-sdk-v2.1.2-0-*.zip and extract the facebook-php-sdk-*/src/facebook.php to the folder ‘fb’;
3. inside ‘fb’, create a php file called ‘index.php’ with the following contents:
<?php require 'facebook.php'; $facebook = new Facebook(array( 'appId' => 'APP_ID', 'secret' => 'APP_SECRET', 'cookie' => true, )); //2. retrieving session $session = $facebook->getSession(); //3. requesting 'me' to API $me = null; if ($session) { try { $uid = $facebook->getUser(); $me = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); } } //4. login or logout if ($me) { $logoutUrl = $facebook->getLogoutUrl(); } else { $loginUrl = $facebook->getLoginUrl(); } ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>php-sdk</title> </head> <body> <?php if ($me): ?> <?php echo "Welcome, ".$me['first_name']. ".<br />"; ?> <a href="<?php echo $logoutUrl; ?>"> <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"> </a> <?php else: ?> <a href="<?php echo $loginUrl; ?>"> <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"> </a> <?php endif ?> </body> </html>
this file is freely based on the example provided in the facebook-php-sdk-v2.1.2-0-*.zip package. you’ll need to replace the APP_ID and the APP_SECRET with the contents you’ve copied before.
in the commented parts of the code, in 1, you’re creating an Facebook object, which will be your bridge to the Facebook webservices; also, you’re specifying that you want to use cookies as session storage method. in 2, you’re retrieving a session; you *need* to do this, so the Facebook object can fetch the session data from the state before. if you do not do this, none of the following commands assuming the existence of a session will work. in 3, you’re doing a simple request to the API, asking for your profile public data. and, finally, in 4, if $me is empty, it means that you’re not logged in, and the variables are set properly.
now, acess http://localhost/fb/index.php and let’s see the magic. the first time ever when you try to login in the webpage, you’ll be redirected to a facebook page where you’ll have to give permission for the app to access your data. this just happens once. and when you click logout, you’re logging out of Facebook.
ok, now lets see some fun stuff. log in and then acess http://localhost/fb/index.php , without all that strange parameters you see on the url when you’re logged in. a strange thing happens: the script does not detect you’re already logged in. you can verify this by going to the facebook home and see you’re actually logged in!
according to this comment, this happens because php does not set cookies correctly for the localhost domain. for this to work, you’ll need to change a little bit the facebook.php file. go to the line 661-663 and replace them with:
if ($domain) { $domain = '.' . $domain; if ($domain == '.localhost') $domain = false; }
and done! now, you can login, access http://localhost/fb/index.php, and the session will be correctly set!
further and very important comments
there an very important comment I have to make: when you logout, the session is still set. when you login, you define a cookie that has an access token, so your application can access your data. to the Facebook object, this access token is available even when you logout (which, in my opinion, should not occur). your script detects that the access token is not valid anymore when it tries to fetch the ‘/me’ data from the API and an exception occurs. if the access token is not valid anymore and you want to make an API request, you receive something like that:
“Fatal error: Uncaught OAuthException: Error validating access token: The session is invalid because the user logged out or because auth.expireSession was invoked.(…)”
to prevent this from happening, you have two choices:
1. specify a return_url for the logout: this is a parameter you specify when invoking the getLogoutUrl() method. this is how you do it, for instance:
$logoutUrl = $facebook->getLogoutUrl(array( 'next'=>'http://localhost/fb/logout.php' ));
and, in this address, you can manually empty the cookie. see the full code for ‘logout.php’:
<?php require 'facebook.php'; $facebook = new Facebook(array( 'appId' =--> 'APP_ID', 'secret' => 'APP_SECRET', 'cookie' => true, )); //ovewrites the cookie $facebook->setSession(null); //redirects to index header('Location: http://localhost/fb/index.php'); ?>
2. empty the cookies everytime a request fails: now, when the user access any page that asks for getSession(), it will be empty, and you won’t need to fetch ‘/me’ from the API to know if the access token is still valid, altough it is a good pratice always surround it with try-catch statements. now, we can make a modification to index.php, so itself can empty the cookie when the access token is not valid anymore. let the try-catch block look like the following:
try { $uid = $facebook->getUser(); $me = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $facebook->setSession(null); }
the benefit of this is, when your user has logged out and it was not through your application, you’ll can empty the cookie and do not make further requests to API.
3. specify a return_url for the login: this will clean the url. when you’re loging in, the facebook will pass a lot of parameters to you scripts that will be parsed by the Facebook object. you can prevent them from showing in your url specifying a returl_url for the login.
$loginUrl = $facebook->getLoginUrl(array( 'next'=>'http://localhost/fb/login.php' ));
and the code for login.php:
<?php require 'facebook.php'; $facebook = new Facebook(array( 'appId' =--> 'APP_ID', 'secret' => 'APP_SECRET', 'cookie' => true, )); $session = $facebook->getSession(); //redirects to index header('Location: http://localhost/fb/index.php'); ?>
4. you can use ‘offline_access’: this provides you a long term access token, so you can access user data and post updates in his profile even when he is not using your app. here you can see an example of how to use it (I didn’t tested it).
of course, there are a LOT of other things you can explore with facebook-php-sdk, the Graph API and a plethora of other webservices that Facebook has to offer. this is a very brief introduction, so you can have a little example running locally and can make your tests. you can check this page for more examples with the Graph API (I didn’t tested it).
you can download the code here:
http://cacovsky.googlecode.com/files/facebook-login-example.zip
(don’t forget to replace APP_ID and APP_SECRET with the appropriate values in config.php!)
useful links:
the facebook php sdk
facebook developers
facebook developer docs
issue with facebook php sdk and localhost cookies
error validating access token
setting return url
authentication permissions
using offline access token
examples with the facebook Graph API
Hello I like the tutorial except I’m testing my appln. on localhost and none of it is working.
I would have tried your fix but I’m running the latest SDK version (3).
Any help would be greatly appreciated!
hi, agadn,
as I stated in the second paragraph of this post, “I’m using the facebook php-sdk 2.1.2”. I still didn’t tested the latest version, but it is something I’m planning.
[…] https://cacovsky.wordpress.com/2011/05/17/login-with-facebook-php-sdk-a-tutorial/ […]
[…] https://cacovsky.wordpress.com/2011/05/17/login-with-facebook-php-sdk-a-tutorial/ […]
Hi
i have test this tutorial but nothing happens, what cause this?
please help
please provide more details.
Good post!
FYI: In the latest version of the SDK, they’ve removed the getSession(); method.
Hi,
i run this code but there is an error
Fatal error: Uncaught exception ‘Exception’ with message ‘Facebook needs the CURL PHP extension.’ in C:\xampp\htdocs\fb\base_facebook.php:19 Stack trace: #0 C:\xampp\htdocs\fb\facebook.php(18): require_once() #1 C:\xampp\htdocs\fb\index.php(2): require(‘C:\xampp\htdocs…’) #2 {main} thrown in C:\xampp\htdocs\fb\base_facebook.php on line 19
as the error clearly says, “Facebook needs the CURL PHP extension”. enable/install the curl extension for php and re-run the sample.
the second comment
$facebook->getSession(); doesn’t return any value so it is giving me 500 internal server error
[…] More Detail Tutorial about login with facebook php sdk – a tutorial […]
it’s a wonderful post! but i have a question, i can’t log out with the button that it’s configured, so, how can i log out just click on the link marked as “log out”??
Reblogged this on Srikanth's Blog.
Very nice tutorial. Thanks!
For those who want the SDK 2:
http://goo.gl/yY0oK
Great post!
How to Authenticate Users using Facebook PHP SDK 3.0 and customize facebook login image. Demo and Source Code available.
http://www.idiotminds.com/login-facebook-using-php-sdk/
Great tutorial! Tnx!
hey guys check this out
http://learnwebscripts.com/how-to-login-into-facebook-account-using-php
its very easy to integrate
Hi there, just became aware of your blog through Google, and found that it is truly informative.
I am gonna watch out for brussels. I will appreciate if you continue this in
future. Lots of people will be benefited from your writing.
Cheers!
Thanks!
Hi , it’s a great tutorial but i have a doubt . I am making this website where the user will have the option to login through fb and twitte too while having the ability to login through registering through my site as well . Can you tell me how to go about it ? Like database specifics ?
No.
facebook get session is not in use now
[…] https://cacovsky.wordpress.com/2011/05/17/login-with-facebook-php-sdk-a-tutorial/ […]
Could you please tell me the lines of code in facebook.php file which we have to replace in order to save cookie correctly for localhost. I am unable to locate 661-663.
Hi,
At the end of the post you can see where to download all the files for this. Take into account that this is a outdated tutorial.
HI,
Nice write-up although its outdated its still informative.
Did you write a similar tutorial using the latest PHP SDK 3.2.2? There are a lot of issues out in there and it would be great if you can write up one for that.
Thanks.
Caleb.
No, I didn’t, and I don’t have any plans to write it, sorry.