Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Facebook PHP SDK v4 & the new Graph API

Facebook PHP SDK v4 & the new Graph API

...and why you're gonna have to rewrite all the things.
Open Spaces talk @ php[tek] on May 22, 2014.

Sammy Kaye Powers

May 22, 2014
Tweet

More Decks by Sammy Kaye Powers

Other Decks in Programming

Transcript

  1. FACEBOOK PHP SDK V4 THE NEW GRAPH API AND WHY

    YOU'RE GONNA HAVE TO REWRITE all the things
  2. MORE NEW STUFFS ▸ 48-hour bug fix commitment ▸ Test

    apps ▸ New review process for extended permissions ▸ Loads more
  3. use Facebook\FacebookSession; use Facebook\FacebookRequest; use Facebook\GraphUser; $session = new FacebookSession('access-token-here');

    $user = (new FacebookRequest( $session, 'GET', '/me?fields=id,name' ))->execute()->getGraphObject(GraphUser::className());
  4. HELPERS GET THE ACCESS TOKEN ▸ FacebookRedirectLoginHelper ...for your website

    ▸ FacebookCanvasLoginHelper ..for canvas apps ▸ FacebookJavaScriptLoginHelper ..cookies from Javascript SDK
  5. $helper = new FacebookRedirectLoginHelper($redirect_url); try { $session = $helper->getSessionFromRedirect(); }

    catch(FacebookRequestException $ex) { // When Facebook returns an error } catch(\Exception $ex) { // When validation fails or other local issues } if ($session) { // Logged in. }
  6. protected function loadState() { if ($this->checkForSessionStatus === true && session_status()

    !== PHP_SESSION_ACTIVE) { throw new FacebookSDKException( 'Session not active, could not load state.', 721 ); } if (isset($_SESSION[$this->sessionPrefix . 'state'])) { $this->state = $_SESSION[$this->sessionPrefix . 'state']; return $this->state; } return null; }
  7. session_status() ▸ PHP_SESSION_DISABLED if sessions are disabled. ▸ PHP_SESSION_NONE if

    sessions are enabled, but none exists. ▸ PHP_SESSION_ACTIVE if sessions are enabled, & one exists.
  8. YOU'LL NEED TWO OBJECTS TO DO IT ▸ FacebookSession the

    access-token object ▸ FacebookRequest handles requests made to Graph
  9. $session = new FacebookSession('access-token-here'); $request = new FacebookRequest( $session, 'GET',

    '/some-object-id?fields=id,name' ); $something = $request->execute()->getGraphObject();
  10. SUB GraphObject'S ▸ GraphUser casts a DateTime() ▸ GraphLocation ▸

    GraphSessionInfo casts some DateTime()'s ▸ new: GraphAlbum casts some DateTime()'s
  11. GIVE ME all the things! ▸ User's Name ▸ Last

    profile update time ▸ The first 4 events that the user is attending & first 2 photos from each event ▸ The first 3 pages the user likes ▸ The first 5 photos the user is tagged in
  12. THERE must BE A BETTER WAY! THERE IS. ENTER FACEBOOK

    QUERY BUILDER HTTPS://GITHUB.COM/SAMMYK/FACEBOOKQUERYBUILDER
  13. // Get first 5 photos the user is tagged in

    $photos_user_tagged_in = $fqb ->edge('photos') ->fields('name', 'source') ->limit(5); // Get first 3 pages this user likes $pages_user_likes = $fqb ->edge('likes') ->fields('name', 'link') ->limit(3); // Get first 4 events that this user is attending // And first 2 photos from each event $event_photos = $fqb ->edge('photos') ->fields('name', 'source') ->limit(2); $events_user_attending = $fqb ->edge('events') ->fields('name', 'start_time', 'end_time', $event_photos) ->limit(4); // Get the logged in user's name, last profile update time, and all those edges $user_data = $fqb->object('me') ->fields('name', 'updated_time', $photos_user_tagged_in, $pages_user_likes, $events_user_attending); ->get();