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