Facebook Authentication Using PHP-SDK

I tried a couple of days with no luck, I was able to use example.php, which comes with the PHP-SDK and worked fine. I just need to save the returned session and use it later so that I can access without re-authentication.

I tried to save the sessions in a serialized field in the database, and then restore the data, non-initialize it and use the setSession function in php-sdk to get authentication. Unfortunately, this did not work,

Here is a link to a previous question with code samples.

Facebook OAuth retrieves information based on saved session

Consult?

+4
source share
1 answer

Update: Offline access is now out of date . Use something else instead.


If you want to use a user's access token even after it offline_access your application, you need to request offline_access permission.

Using PHP PHP SDK v3 ( see on github ), it's pretty simple. To register someone with offline_access permission, you request it when you create the login URL. This is how you do it.

Get Internet Access Token

First you check if the user is registered or not:

 require "facebook.php"; $facebook = new Facebook(array( 'appId' => YOUR_APP_ID, 'secret' => YOUR_APP_SECRET, )); $user = $facebook->getUser(); if ($user) { try { $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { $user = null; } } 

If this is not the case, you create a Facebook Login URL requesting offline_access permission:

 if (!$user) { $args['scope'] = 'offline_access'; $loginUrl = $facebook->getLoginUrl($args); } 

And then display the link in the template:

 <?php if (!$user): ?> <a href="<?php echo $loginUrl ?>">Login with Facebook</a> <?php endif ?> 

Then you can get the access token and save it. To get it, call:

 if ($user) { $token = $facebook->getAccessToken(); // store token } 

Use network access token

Use the network access token when the user is not logged in:

 require "facebook.php"; $facebook = new Facebook(array( 'appId' => YOUR_APP_ID, 'secret' => YOUR_APP_SECRET, )); $facebook->setAccessToken("..."); 

And now you can make API calls for this user:

 $user_profile = $facebook->api('/me'); 

Hope this helps!

+7
source

All Articles