Categories: APIDevelopmentPHP

Facebook API / PHP SDK / Login & more

Will be adding some basic information about using the current Facebook API with the PHP SDK.  Have been updating a lot of old API integrations over the past 2 months and finally solved many badly documented areas of the new API. Will be updating this in the future, as I dive deeper into the latest API changes.

Including the SDK (Github)

  1. require_once _DIR_TO_AUTOLOAD.'/facebook_sdk/autoload.php';
  2.  
  3. use Facebook\FacebookSession;
  4. use Facebook\FacebookRedirectLoginHelper;
  5. use Facebook\FacebookRequest;
  6. use Facebook\FacebookResponse;
  7. use Facebook\FacebookSDKException;
  8. use Facebook\FacebookRequestException;
  9. use Facebook\FacebookAuthorizationException;
  10. use Facebook\GraphObject;
  11. use Facebook\Entities\AccessToken;
  12. use Facebook\HttpClients\FacebookCurlHttpClient;
  13. use Facebook\HttpClients\FacebookHttpable;

Depending on what you want to do with the API, the above needs to be adjusted. Check the Facebook API documentation for more details.

Basic Login / User data retrieval

  1. // Session should be started after all files have been included
  2.  
  3. // init app with app id and secret
  4. FacebookSession::setDefaultApplication( _FB_APP_ID_, _FB_APP_SECRET_ );
  5.  
  6. // login helper with redirect_uri
  7. $helper = new FacebookRedirectLoginHelper( 'http://yourdomain.com/FB_LOGIN/');
  8.  
  9. // Check for Facebook redirect & retrieve session
  10. try {
  11.   $session = $helper->getSessionFromRedirect();
  12. } catch( FacebookRequestException $ex ) {
  13.   // When Facebook returns an error
  14. } catch( Exception $ex ) {
  15.   // When validation fails or other local issues
  16. }
  17.  
  18.  
  19. if ( isset( $session ) ) {
  20. /*
  21. Store the session for later reuse, in this case even getting a long lived session. You should store this in your database.
  22. The session can be reused with:
  23. $session = new FacebookSession($_SESSION['long']->getToken());
  24. */
  25.  
  26. if(empty($_SESSION['long'] )){
  27.         $_SESSION['long'] = $session->getLongLivedSession();
  28. }
  29. // graph api request for user data
  30.  
  31. try {
  32.  
  33.     $user_profile = (new FacebookRequest(
  34.       $session, 'GET', '/me'
  35.     ))->execute()->getGraphObject(GraphUser::className());
  36.  
  37.     echo "Name: " . $user_profile->getName();
  38.  
  39.   } catch(FacebookRequestException $e) {
  40.  
  41.     echo "Exception occured, code: " . $e->getCode();
  42.     echo " with message: " . $e->getMessage();
  43.  
  44.   }  
  45. } else {
  46.   // show login url if no session available
  47.   // request special permissions, optional.
  48. $params = array(
  49.   'manage_pages', 'publish_pages','publish_actions','user_videos'
  50. );
  51.   echo '<a href="' . $helper->getLoginUrl($params ) . '">Login</a>';
  52. }

 

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

4 weeks ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago

Archaeology Travel Booth – Travel Innovation Summit 2023

Archaeology Travel is an online travel guide for people who enjoy exploring the world’s pasts.… Read More

6 months ago