All about the new, NEW Facebook PHP SDK v4.1

All about the new, NEW Facebook PHP SDK v4.1

A 10 minute lightning talk presented at Nomad PHP on March 19th, 2015.

When developing on Facebook, things change all the time. And things are about to change once again with the release of the Facebook PHP SDK v4.1. It's got tons of new powerful features that have never existed in a Facebook PHP SDK before. This lightning talk is focused on giving a crash-course on the new Facebook PHP SDK v4.1 and it's features. By the end you should have the confidence to start using the new SDK within your own app.

8c090cc1ccd623a146ddd9159b1bf7e2?s=128

Sammy Kaye Powers

March 19, 2015
Tweet

Transcript

  1. All About the By @SammyK For @nomadphp On March 19th,

    2015 new, NEW Facebook PHP SDK 4.1
  2. Does not work @

  3. Goal: overview To get an

  4. Facebook PHP SDK 4.1 version history 4.o 3.x

  5. Facebook PHP SDK 4.1 do you even need it?

  6. OAuth 2.0 Facebook Login uses

  7. OAuth 2.0 Client $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);
  8. Socialite Laravel 5 public function redirectToProvider() {! return Socialize::with(‘facebook')! !

    ! ! ->redirect();! }! ! public function handleProviderCallback() {! $user = Socialize::with(‘facebook')! ! ! ! ->user();! // $user->token;! }
  9. • Signed request support • Easy-as-pie file uploads • Easy

    batch request support • Easy-peasy pagination • Graph Nodes as collections Facebook PHP SDK
  10. Implementation

  11. "require": {! "facebook/php-sdk-v4":! "~4.1.0@dev"! } composer.json Installation

  12. "require": {! "facebook/php-sdk-v4":! "~4.1.0@dev"! } composer.json Installation

  13. "require": {! "facebook/php-sdk-v4":! "~4.1.0@dev"! } composer.json Installation

  14. Usage

  15. Facebook\Facebook() Usage all the things does

  16. $fb Usage we instantiated as

  17. Facebook\Facebook() • Handles fall-back config • Factory for helpers &

    entities • Helps with HTTP requests • Container to inject custom deps
  18. $fb = new Facebook\Facebook([! /* */! ]); Configuration

  19. $fb = new Facebook\Facebook([! 'app_id' => 'app-id',! 'app_secret' => 'secret',!

    ]); Configuration
  20. $fb = new Facebook\Facebook([! 'app_id' => 'app-id',! 'app_secret' => 'secret',!

    ]); SECRET! better: use env vars Configuration
  21. $fb = new Facebook\Facebook(); if you have env vars FACEBOOK_APP_ID

    FACEBOOK_APP_SECRET and Configuration
  22. Authentication

  23. Authentication Facebook Login

  24. login from redirect $helper = $fb->getRedirectLoginHelper();! $login_link = $helper->! getLoginUrl(‘https://foo.com/fb');!

    ! echo '<a href="' . $login_link . '">'.! 'Log in!</a>'; Route: / Authentication
  25. callback URL Route: /facebook/login $token = $helper->getAccessToken(); Authentication

  26. $token once you obtain user is logged in! :) (sort

    of…) Authentication
  27. Store it for later… session Database $token Authentication

  28. Requests & Responses

  29. Requests & Responses GET POST DELETE $fb->get(); PUT & PATCH

    $fb->post(); $fb->delete();
  30. get(), post(), delete() Facebook Request

  31. get(), post(), delete() Facebook Request Facebook Client

  32. get(), post(), delete() Facebook Request Facebook Client Graph API

  33. get(), post(), delete() Facebook Request Facebook Client Graph API

  34. get(), post(), delete() Facebook Request Facebook Client Graph API Facebook

    Response
  35. Request & Response GET $fb->get();

  36. $res = $fb->get(‘/123’, $token); $fb = Facebook\Facebook() Request & Response

    GET
  37. $res = $fb->get(‘/123’, $token); User node id $fb = Facebook\Facebook()

    Request & Response GET
  38. $res = $fb->get(‘/123’, $token); Can set Default $fb = Facebook\Facebook()

    Request & Response GET
  39. $fb->! setDefaultAccessToken($token);! $res = $fb->get(‘/123’); Set As Default $fb =

    Facebook\Facebook() Request & Response GET
  40. $fb->! setDefaultAccessToken($token);! $res = $fb->get(‘/123’); don’t need $fb = Facebook\Facebook()

    Request & Response GET
  41. $res = $fb->get(‘/123’); Facebook\FacebookResponse() Facebook Response $fb = Facebook\Facebook() Request

    & Response GET
  42. Response Data Handling the Facebook Response

  43. $data = $res->getBody(); Facebook Response Response Data Plain- old string

    $res = Facebook\FacebookResponse()
  44. $data = $res->getDecodedBody(); Facebook Response Response Data Plain- old array();

    $res = Facebook\FacebookResponse()
  45. Response Collections Graph Nodes

  46. NoDES?

  47. Graph Theory It’s from

  48. None
  49. Dots

  50. Dots Lines

  51. Dots Lines

  52. Nodes Lines

  53. Nodes Lines

  54. Nodes Edges

  55. None
  56. A “Thing”

  57. the relation between the things

  58. Terminology uses the same Graph API

  59. Terminology uses the same Graph API Those Are NoDES!

  60. Response Collections Graph Nodes

  61. $node = $res->getGraphObject(); $res = Facebook\FacebookResponse() Facebook Response Response Data

    Graph! Object
  62. $node = $res->getGraphObject(); Facebook\GraphNodes\GraphObject() Heads up $res = Facebook\FacebookResponse() Facebook

    Response Response Data
  63. $node = $res->getGraphNode(); Facebook\GraphNodes\GraphNode() Heads up $res = Facebook\FacebookResponse() Facebook

    Response Response Data
  64. Facebook\! GraphNodes\! GraphNode() extends from a collection Graph! Object/! Node

  65. Graph Edges

  66. $res = $fb->get(‘/me/photos’); Facebook Response $fb = Facebook\Facebook() Graph Edges

  67. $res = $fb->get(‘/me/photos’); $fb = Facebook\Facebook() Graph Edges $node =

    $res->getGraphObject();
  68. $res = $fb->get(‘/me/photos’); $fb = Facebook\Facebook() Graph Edges $node =

    $res->getGraphObject(); Nope
  69. $res = $fb->get(‘/me/photos’); $fb = Facebook\Facebook() Graph Edges $edge =

    $res->getGraphList(); GraphList
  70. $res = $fb->get(‘/me/photos’); $fb = Facebook\Facebook() Graph Edges $edge =

    $res->getGraphList(); GraphList Heads up
  71. $res = $fb->get(‘/me/photos’); $fb = Facebook\Facebook() Graph Edges $edge =

    $res->getGraphEdge(); GraphList / Edge Heads up
  72. $nextEdge = $fb->next($edge);
 $prevEdge = $fb->previous($edge); $edge = Facebook\GraphNodes\GraphList() Graph

    Edges GraphList / Edge Pagination Example
  73. $nextEdge = $fb->next($edge);
 $prevEdge = $fb->previous($edge); $edge = Facebook\GraphNodes\GraphList() Graph

    Edges GraphList / Edge Pagination Example Another! GraphList / Edge
  74. Request & Response POST $fb->post();

  75. $data = [! ! ! ‘message’ => ‘Hi from Nomad

    PHP!’,! ];! ! $res = $fb->post(‘/me/feed’, $data); $fb = Facebook\Facebook() Request & Response POST
  76. Request & Response DELETE $fb->delete();

  77. $res = $fb->delete(‘/123’); $fb = Facebook\Facebook() Request & Response DELETE

  78. Integration Advanced

  79. $fb = new Facebook\Facebook([! ‘persistent_data_handler'! => $myPersistentHandler,! ]); Advanced Integration

  80. $fb = new Facebook\Facebook([! ‘persistent_data_handler'! => $myPersistentHandler,! ]); Advanced Integration

    instance of PersistentDataInterface
  81. Injectable Persistent Data Storage: `PersistentDataInterface` Injectable HTTP Client: `FacebookHttpClientInterface` Injectable

    CSPRNG: `PseudoRandomStringGeneratorInterface` Injectable URL detection: `UrlDetectionInterface` Advanced Integration
  82. File Uploads

  83. $data = [
 'message' => 'My awesome photo.',
 'source' =>

    $fb->fileToUpload('/my/photo.jpg'),
 ];! ! $res = $fb->post(‘/me/feed’, $data); $fb = Facebook\Facebook() File Uploads Photo example
  84. $data = [! 'title' => 'My video’,
 'description' => 'My

    awesome video.',
 'source' => $fb->videoToUpload('/my/photo.mp4'),
 ];! ! $res = $fb->post(‘/me/feed’, $data); $fb = Facebook\Facebook() File Uploads Video example
  85. Getting Involved

  86. Getting Involved https://github.com/facebook/facebook-php-sdk-v4

  87. None
  88. Fosco Marotto

  89. FACEBOOK D-DAY Photo: @darthmauldds on Flickr APRIL 30 2015

  90. March 25 & 26 San Francisco developers.facebook.com

  91. THANKS! Sammy Kaye Powers @SammyK SammyK.me Host of @PHPRoundtable Chicago

    PHP UG West Coast Swing