$30 off During Our Annual Pro Sale. View Details »

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.

Sammy Kaye Powers

March 19, 2015
Tweet

More Decks by Sammy Kaye Powers

Other Decks in Education

Transcript

  1. All About the
    By @SammyK
    For @nomadphp
    On March 19th, 2015
    new, NEW
    Facebook
    PHP SDK
    4.1

    View Slide

  2. Does not
    work @

    View Slide

  3. Goal:
    overview
    To get an

    View Slide

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

    View Slide

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

    View Slide

  6. OAuth 2.0
    Facebook Login
    uses

    View Slide

  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);

    View Slide

  8. Socialite
    Laravel 5
    public function redirectToProvider() {!
    return Socialize::with(‘facebook')!
    ! ! ! ->redirect();!
    }!
    !
    public function handleProviderCallback() {!
    $user = Socialize::with(‘facebook')!
    ! ! ! ->user();!
    // $user->token;!
    }

    View Slide

  9. • Signed request support
    • Easy-as-pie file uploads
    • Easy batch request support
    • Easy-peasy pagination
    • Graph Nodes as collections
    Facebook PHP SDK

    View Slide

  10. Implementation

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  14. Usage

    View Slide

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

    View Slide

  16. $fb
    Usage
    we instantiated as

    View Slide

  17. Facebook\Facebook()
    • Handles fall-back config
    • Factory for helpers & entities
    • Helps with HTTP requests
    • Container to inject custom deps

    View Slide

  18. $fb = new Facebook\Facebook([!
    /* */!
    ]);
    Configuration

    View Slide

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

    View Slide

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

    View Slide

  21. $fb = new Facebook\Facebook();
    if you have env vars
    FACEBOOK_APP_ID
    FACEBOOK_APP_SECRET
    and
    Configuration

    View Slide

  22. Authentication

    View Slide

  23. Authentication
    Facebook
    Login

    View Slide

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

    View Slide

  25. callback URL
    Route: /facebook/login
    $token = $helper->getAccessToken();
    Authentication

    View Slide

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

    View Slide

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

    View Slide

  28. Requests & Responses

    View Slide

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

    View Slide

  30. get(), post(), delete()
    Facebook
    Request

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  42. Response Data
    Handling the
    Facebook
    Response

    View Slide

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

    View Slide

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

    View Slide

  45. Response Collections
    Graph Nodes

    View Slide

  46. NoDES?

    View Slide

  47. Graph
    Theory
    It’s from

    View Slide

  48. View Slide

  49. Dots

    View Slide

  50. Dots Lines

    View Slide

  51. Dots Lines

    View Slide

  52. Nodes Lines

    View Slide

  53. Nodes Lines

    View Slide

  54. Nodes Edges

    View Slide

  55. View Slide

  56. A “Thing”

    View Slide

  57. the relation
    between the things

    View Slide

  58. Terminology
    uses the same
    Graph API

    View Slide

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

    View Slide

  60. Response Collections
    Graph Nodes

    View Slide

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

    View Slide

  62. $node = $res->getGraphObject();
    Facebook\GraphNodes\GraphObject()
    Heads up
    $res = Facebook\FacebookResponse()
    Facebook
    Response
    Response Data

    View Slide

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

    View Slide

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

    View Slide

  65. Graph Edges

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  72. $nextEdge = $fb->next($edge);

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

    View Slide

  73. $nextEdge = $fb->next($edge);

    $prevEdge = $fb->previous($edge);
    $edge = Facebook\GraphNodes\GraphList()
    Graph Edges
    GraphList
    / Edge
    Pagination Example
    Another!
    GraphList
    / Edge

    View Slide

  74. Request & Response
    POST
    $fb->post();

    View Slide

  75. $data = [!
    ! ! ‘message’ => ‘Hi from Nomad PHP!’,!
    ];!
    !
    $res = $fb->post(‘/me/feed’, $data);
    $fb = Facebook\Facebook()
    Request & Response
    POST

    View Slide

  76. Request & Response
    DELETE
    $fb->delete();

    View Slide

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

    View Slide

  78. Integration
    Advanced

    View Slide

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

    View Slide

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

    View Slide

  81. Injectable Persistent Data Storage: `PersistentDataInterface`
    Injectable HTTP Client: `FacebookHttpClientInterface`
    Injectable CSPRNG: `PseudoRandomStringGeneratorInterface`
    Injectable URL detection: `UrlDetectionInterface`
    Advanced Integration

    View Slide

  82. File Uploads

    View Slide

  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

    View Slide

  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

    View Slide

  85. Getting Involved

    View Slide

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

    View Slide

  87. View Slide

  88. Fosco Marotto

    View Slide

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

    View Slide

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

    View Slide

  91. THANKS!
    Sammy Kaye Powers
    @SammyK
    SammyK.me
    Host of @PHPRoundtable
    Chicago PHP UG
    West Coast Swing

    View Slide