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

An entertaining intro to Graph Theory, the Face...

An entertaining intro to Graph Theory, the Facebook Platform and the new, NEW PHP SDK

An uncon talk at Sunshine PHP 2015

Sammy Kaye Powers

February 07, 2015
Tweet

More Decks by Sammy Kaye Powers

Other Decks in Education

Transcript

  1. Sammy Kaye Powers February 7th, 2015 new, new An entertaining

    intro to Platform Graph theory Facebook the and the PHP SDK
  2. No.

  3. HTTP API over HTTP/1.1 200 OK! Content-Type: application/json! ! {!

    !“id”: 123,! !“name”: “Foo User”! }
  4. Graph API The • REST! • HATEOAS! • OAuth! •

    etc… Nope Nope ✓ Yep! (2.0)
  5. Graph API The • REST! • HATEOAS! • OAuth! •

    etc… Nope Nope ✓ Yep! (2.0) Don’t have to know!
  6. Graph API Can I haz user 123? Can I haz

    user 123? Social Graph PHP App
  7. photo id:4590 HTTP/1.1 200 OK! {! "id": "4590", ! "name":

    "Plane!", ! . . .! } Host: graph.facebook.com! GET /v2.2/4590
  8. photo id:4590 GET /v2.2/4590/comments HTTP/1.1 200 OK! ! {"data": [!

    {"id": "4590_123",! "message": "Pickles!"},! {"id": "4590_456", ! "message": "Turtles!"}! ]} comments
  9. User Node fields edges - id! - first_name! - last_name!

    - name! - email * /albums! /feed! /photos! /friends! /picture *
  10. 1. Read the Platform Policy 2. Read the Platform Policy

    3. Read the Platform Policy Three important points
  11. ID

  12. Ah?

  13. Ah?

  14. Ah?

  15. Login Flow • User clicks on link • Facebook asks

    to approve • Access token is obtained *
  16. Clients OAuth 2.0 PHP League $authUrl = $provider->getAuthorizationUrl();! $_SESSION['oauth2state'] =

    $provider->state;! header('Location: '.$authUrl);! ! // . . .! ! $token = $provider->getAccessToken(! ! 'authorization_code', [! 'code' => $_GET['code']! ]);! $user = $provider->getUserDetails($token);! printf('Hello %s!', $userDetails->firstName);
  17. Clients OAuth 2.0 Laravel 5 public function redirectToProvider() {! return

    Socialize::with(‘facebook')! ! ! ! ->redirect();! }! ! public function handleProviderCallback() {! $user = Socialize::with(‘facebook')! ! ! ! ->user();! // $user->token;! }
  18. php SDK v4.1 • Handles login flow • Graph Nodes

    as collections • Signed request support • Easy-as-pie file uploads • Easy batch request support • Easy-peasy (deep) pagination
  19. Config $fb = new Facebook\Facebook(); if you have env vars

    FACEBOOK_APP_ID FACEBOOK_APP_SECRET and
  20. login from redirect $login_link = $fb->getLoginUrl();! ! echo '<a href="'

    . $login_link . '">'.! 'Log in!</a>'; Route: / php SDK v4.1
  21. callback URL use Facebook\Exceptions\FacebookSDKException;! try {! $token = $fb->getAccessTokenFromRedirect();! }

    catch (FacebookSDKException $e) {! dd(‘Error:' . $e->getMessage());! } Route: /facebook/login php SDK v4.1
  22. Getting User Data $a = $res->getHttpStatusCode();! $b = $res->getHeaders();! $c

    = $res->getBody();! $d = $res->getDecodedBody(); Facebook\FacebookResponse()
  23. Getting User Data GraphUser! GraphPage! GraphAlbum Facebook\GraphNodes\GraphNode() sub nodes of

    namespace: Facebook\GraphNodes $res->getGraphUser();! $res->getGraphPage();! $res->getGraphAlbum();
  24. User Node fields edges - id! - first_name! - last_name!

    - name! - email * /albums! /feed! /photos! /friends! /picture *
  25. Getting User Data $id = $userNode->getId();! $name = $userNode->getName();! $partner

    =! $userNode->getSignificantOther(); Facebook\GraphNodes\GraphUser()
  26. Getting User Data $data = [! ! ! ‘message’ =>

    ‘Hello from #ssp15’,! ];! ! $res = $fb->post(‘/me/feed’, $data); Facebook\Facebook()
  27. Getting User Data $data = [! ! ! ‘message’ =>

    ‘Hello from #ssp15’,! ];! ! $res = $fb->post(‘/me/feed’, $data); Facebook\Facebook()
  28. Javascript SDK installation <script>! window.fbAsyncInit = function() {! FB.init({! appId

    : 'your-app-id',! xfbml : true,! version : 'v2.1'! });! };! ! (function(d, s, id){! var js, fjs = d.getElementsByTagName(s)[0];! if (d.getElementById(id)) {return;}! js = d.createElement(s); js.id = id;! js.src = "//connect.facebook.net/en_US/sdk.js";! fjs.parentNode.insertBefore(js, fjs);! }(document, 'script', 'facebook-jssdk'));! </script> copy/paste before developers.facebook.com/docs/javascript/quickstart </body>
  29. Javascript SDK installation <script>! window.fbAsyncInit = function() {! FB.init({! appId

    : 'your-app-id',! xfbml : true,! version : 'v2.1'! });! };! ! (function(d, s, id){! var js, fjs = d.getElementsByTagName(s)[0];! if (d.getElementById(id)) {return;}! js = d.createElement(s); js.id = id;! js.src = "//connect.facebook.net/en_US/sdk.js";! fjs.parentNode.insertBefore(js, fjs);! }(document, 'script', 'facebook-jssdk'));! </script> developers.facebook.com/docs/javascript/quickstart This
  30. meta tags <meta property=“og:url"! content="http://foo" /> ! <meta property=“og:title"! content=“Foo

    Site" />! <meta property=“og:description"! content=“Foo description of bar" /> ! <meta property=“og:image"! content=“http://foo.c/bar.jpg” /> Open Graph
  31. meta tags <meta property=“og:url"! content="http://foo" /> ! <meta property=“og:title"! content=“Foo

    Site" />! <meta property=“og:description"! content=“Foo description of bar" /> ! <meta property=“og:image"! content=“http://foo.c/bar.jpg” /> Open Graph
  32. meta tags <meta property=“og:url"! content="http://foo" /> ! <meta property=“og:title"! content=“Foo

    Site" />! <meta property=“og:description"! content=“Foo description of bar" /> ! <meta property=“og:image"! content=“http://foo.c/bar.jpg” /> Open Graph
  33. meta tags <meta property=“og:url"! content="http://foo" /> ! <meta property=“og:title"! content=“Foo

    Site" />! <meta property=“og:description"! content=“Foo description of bar" /> ! <meta property=“og:image"! content=“http://foo.c/bar.jpg” /> Open Graph
  34. Know You prolly didn’t Facebook App Graph API Facebook Developer

    Facebook Login Access Tokens Javascript SDK php SDK
  35. Learn! So much more to • Graph API • User

    Permissions • App Canvas • Page Tabs • Test Users • Test Apps • And more!