Facebook PHP SDK v5

Embed Size (px)

Citation preview

  • 7/25/2019 Facebook PHP SDK v5

    1/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 1/26

    Ben Marshall (Https://Benmarshall.Me/Author/Bmarshall/) May 30, 2014

    Development(Https://Benmarshall.Me/Category/Development/)

    41Comments (Https://Benmarshall.Me/Facebook-Php-Sdk/#Comments)

    Facebook PHP SDK v5.x (4.1) a complete guide!

    (https://benmarshall.me/facebook-php-sdk/)

    Implement the Facebook SDK 5.x (4.1) for PHP (https://github.com/facebook/facebook-php-sdk-v4)to

    enable one-click registrations & logins, gain access to user photos, timelines and even publish to profiles.

    Update (April 29, 2015):Facebook PHP SDK 5.x? What happened to 4.1 you ask? The Facebook PHP SDK

    will start following SemVer starting with v5.0 (https://github.com/facebook/facebook-php-sdk-

    v4/issues/411#issuecomment-97556280). Before the decision to adopt SemVer, the official FacebookPHP SDK was going to be released as v4.1. But now v4.1 is just an alias to v5.0.0. So any references to 4.1

    can be safely aliased to v5.0.0. For more information, see F8 2015 Facebook Developer Conference and

    the new PHP SDK (https://www.phproundtable.com/episode/f8-2015-facebook-developer-conference-

    and-the-new-php-sdk).

    Facebook PHP SDK Complete Guide

    Installing the Facebook PHP SDK

    Facebook PHP SDK Requirements

    Installing the PHP Facebook SDK

    Composer Installation

    Manual Installation

    Using the Facebook PHP SDK

    Initializing the PHP Facebook SDK

    Authentication & Authorization

    Checking If A User Is Logged In

    Getting Access Tokens

    Access Tokens from Facebook Login (manual redirect)

    Access Tokens from App Canvas/Page Tabs

    Access Tokens from the JavaScript SDK

    Getting a Long-Lived (Extended) Access TokenApplication Permissions

    Making Requests to the Graph API

    Handling Exceptions

    http://-/?-http://-/?-https://www.phproundtable.com/episode/f8-2015-facebook-developer-conference-and-the-new-php-sdkhttps://www.phproundtable.com/episode/f8-2015-facebook-developer-conference-and-the-new-php-sdkhttps://www.phproundtable.com/episode/f8-2015-facebook-developer-conference-and-the-new-php-sdkhttps://www.phproundtable.com/episode/f8-2015-facebook-developer-conference-and-the-new-php-sdkhttps://github.com/facebook/facebook-php-sdk-v4/issues/411#issuecomment-97556280https://github.com/facebook/facebook-php-sdk-v4/issues/411#issuecomment-97556280https://github.com/facebook/facebook-php-sdk-v4http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-https://www.phproundtable.com/episode/f8-2015-facebook-developer-conference-and-the-new-php-sdkhttps://github.com/facebook/facebook-php-sdk-v4/issues/411#issuecomment-97556280https://github.com/facebook/facebook-php-sdk-v4https://benmarshall.me/facebook-php-sdk/https://benmarshall.me/category/development/https://benmarshall.me/author/bmarshall/
  • 7/25/2019 Facebook PHP SDK v5

    2/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 2/26

    Facebook PHP SDK Examples

    One-Click Registration & Login

    Log a User Out

    Retrieve Users Profile Information

    Get Users Profile Picture

    Publish to Users Timeline

    Retrieve Users Timeline

    Upload a File

    Upload a Photo

    Upload a Video

    Download Demo Files (Facebook PHP SDK v4) (https://github.com/bmarshall511/demos/tree/master/facebo

    Facebook PHP SDK Requirements

    Version 5 of the SDK runs on PHP 5.4 or greater with the mbstring extension

    (http://php.net/manual/en/mbstring.installation.php)installed.

    Facebook Application Prerequisite

    Youll need to create an app in Facebook (http://developers.facebook.com/apps)in order to use the Facebook

    PHP SDK. Once created, youll be provided with an App IDand App Secret. For more information on creating an

    app, see Facebooks Creating App Details (https://developers.facebook.com/docs/games/appcenter/tutorial)

    tutorial.

    IMPORTANT:Dont forget to set the App Domains when creating an app to where your code is hosted.

    PRO TIP:During development, its a good idea to create a Test App

    (https://developers.facebook.com/docs/apps/test-apps/). This is especially useful when working on a

    local environment. Test Apps have their own App ID, App Secret and settings. This allows you to set App

    Domains to your local environment without affecting production.

    Installing the PHP Facebook SDK

    Composer Installation

    I recommended installing the Facebook PHP SDK with Composer (https://getcomposer.org/). Start by creating a

    file named composer.json and place it in the docroot of your web app. Add the following lines to it:

    1 {

    https://getcomposer.org/https://developers.facebook.com/docs/apps/test-apps/https://developers.facebook.com/docs/games/appcenter/tutorialhttp://developers.facebook.com/appshttp://php.net/manual/en/mbstring.installation.phphttps://github.com/bmarshall511/demos/tree/master/facebook-sdk-php-v4http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-http://-/?-
  • 7/25/2019 Facebook PHP SDK v5

    3/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 3/26

    Versioning Note:As of writing, v5 is still in development so you need to use the @dev minimum-stability

    flag. In addition, the Facebook PHP SDK v4 did not follow Semantic Versioning (http://semver.org/)

    (SemVer). The version schema for the SDK in v4 was 4.MAJOR.MINOR|PATCH . Thats why you would have to

    use 3 version numbers with the ~ operator like ~4.0.0 . But starting with v5, the SDK follows SemVer so

    we just use 2 version numbers: ~5.0 .

    In Terminal from the projects docroot (same directory we placed the composer.json file), run:

    This will download the Facebook PHP SDK v5.

    Facebook PHP SDK v4: Composer Installation

    Manual Installation

    You dont use Composer? You should! But if youre needing a quick-and-dirty install, download the zip from

    GitHub (https://github.com/facebook/facebook-php-sdk-v4/archive/master.zip)and extract the files on your

    computer somewhere.

    Move the folder located at src/Facebook to the location in your web app where you store 3rd-party vendor files.

    You can also rename the folder to facebookphpsdkv5 or something.

    Initializing the PHP Facebook SDK

    Now that we have the dependencies downloaded, were going to initialize the Facebook PHP SDK. Create a file in

    your web app docroot and name it app.php . If you used Composer to install the Facebook PHP SDK, include the

    Composer autoloader (https://getcomposer.org/doc/01-basic-usage.md#autoloading):

    2345

    "require": { "facebook/phpsdkv4": "~5.0@dev" }}

    1 composer update

    123456789

    $ composer updateLoading composer repositories with package informationUpdating dependencies (including requiredev) Installing facebook/phpsdkv4(devmaster cde1d8b) Cloning cde1d8b9f32fc375a8748838eaac1b6d4f48fa5efacebook/phpsdkv4suggests installing guzzlehttp/guzzle(Allows forimplementation of thWriting lock fileGenerating autoload files

    12345

  • 7/25/2019 Facebook PHP SDK v5

    4/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 4/26

    If you manually installed the SDK and assuming you moved and renamed the src/Facebook folder to

    /my/app/third_party/facebookphpsdkv5 , in your web frameworks bootstrap scripts or at the top of your PHP

    script, you need to include the autoloader.

    Important:Dont forget to pass the server session data over if you havent already. This keeps the users

    information stored so the permissions persist as the user navigates your app.

    To initialize the SDK, we need to pass the app_id , app_secret and default_graph_version .

    Youll notice that v5 is namespaced (http://php.net/manual/en/language.namespaces.php)under the

    Facebook\ namespace.

    At first glance you might think v5 also has one big base class that does all the things. In reality, v5 consists of

    many, many different classes and the Facebook\Facebook just ties them all together.

    Be sure to change the {appid} and {appsecret} to your Facebook credentials provided when you created the

    app.

    Facebook PHP SDK v4: Initializing the PHP Facebook SDK

    Authentication & Authorization

    Checking If A User Is Logged In

    If a signed request can be detected, decoded, validated and it contains a user_id in the payload, then the user

    has previously authenticated the app and can be considered logged in. You do this with helpers.

    6 require_once( 'vendor/autoload.php');

    1

    2345

    // Pass session data over.

    session_start();// Include the required dependencies.require( __DIR__.'/third_party/facebookphpsdkv5/autoload.php');

    123456

    // Initialize the Facebook PHP SDK v5.$fb= newFacebook\Facebook([ 'app_id' => '{appid}', 'app_secret' => '{appsecret}', 'default_graph_version'=> 'v2.3',]);

    12345678

    # Facebook PHP SDK v5: Check Login Status Example// Choose your app context helper$helper= $fb>getCanvasHelper();//$helper = $fb>getPageTabHelper();//$helper = $fb>getJavaScriptHelper();// Grab the signed request entity

    http://php.net/manual/en/language.namespaces.php
  • 7/25/2019 Facebook PHP SDK v5

    5/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 5/26

    Getting Access Tokens

    Access Tokens from Facebook Login (manual redirect)

    You can implement Facebook Login (OAuth 2.0) with the PHP SDK by using the FacebookRedirectLoginHelper

    (https://developers.facebook.com/docs/php/FacebookRedirectLoginHelper). An instance of this helper can be

    obtained from the Facebook super service by using the getRedirectLoginHelper() method.

    Facebook PHP SDK v4: Create instance of FacebookRedirectLoginHelper

    Once you get an instance of the FacebookRedirectLoginHelper , you can use it to generate an authorization URL tostart an OAuth 2.0 flow. You pass to this method, the redirect URL (where the user is redirected to after approving

    or denying the app authorization request) and an array of permissions you want to ask them for.

    Now you can grab an instance of the FacebookRedirectLoginHelper again and call the getAccessToken() method

    once the user has clicked on the login link.

    This one-liner is fine if all goes well, but there are a number of possible scenarios our callback script should

    account for.

    1. If all goes well, $accessToken will be an instance of the AccessToken entity. (See The AccessToken Entity

    section below.)

    2. If there was a problem communicating with the Graph API, the getAccessToken() method will throw a

    FacebookSDKException , so youll want to wrap that bad boy with a try/catch to catch the exception and

    display a proper error message to the user.

    91011121314151617181920

    2122232425

    $sr= $helper>getSignedRequest();// Get the user ID if signed request exists$user= $sr? $sr>getUserId() : null;if( $user) { try{

    // Get the access token $accessToken= $helper>getAccessToken(); } catch( Facebook\Exceptions\FacebookSDKException $e) {

    // There was an error communicating with Graph echo$e>getMessage(); exit; }}

    123

    $fb= newFacebook\Facebook([/* . . . */]);$helper= $fb>getRedirectLoginHelper();

    1234567

    $helper= $fb>getRedirectLoginHelper();$permissions= ['email', 'user_posts']; // optional$callback = 'http://example.com/app.php (http://example.com/app.php)';$loginUrl = $helper>getLoginUrl($callback, $permissions);echo'Log in with Facebook!';

    1 $accessToken= $helper>getAccessToken();

    http://example.com/app.phphttps://developers.facebook.com/docs/php/FacebookRedirectLoginHelper
  • 7/25/2019 Facebook PHP SDK v5

    6/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 6/26

    3. If the user denied the request, $accessToken will be null . What this really means is that the SDK could not

    find an OAuth code in the GET params. Keep reading.

    4. If there is no code param, there should be a number of error_* params that describe why theres no code.

    The error params can be accessed using the following methods: getError() , getErrorCode() ,

    getErrorReason() , and getErrorDescription() . We can assume that if getError() returns null , then all

    the other error getter methods will also return null .

    5. In the event that both getAccessToken() and getError() methods return null , then whoever is hitting your

    callback endpoint aint coming from Facebook. In this situation I generally return a 400 Bad Request HTTPresponse. Its also probably a good idea to log this suspicious request.

    If we update our callback script to account for all these scenarios, we get a lot more bullet-proof code.

    This will output a link users can click on to login through Facebook. When clicked, the use will be redirect to

    Facebook to log in if not already. Once logged it, users will be directed back to our redirect URL with an

    appended code parameter that well use to retrieve and store the access token. If all worked, you should seeSuccessfully logged in!

    Facebook PHP SDK v4: Access Tokens from Facebook Login (manual redirect)

    Access Tokens from App Canvas/Page Tabs

    When you have an app that runs in app canvas or a Page tab, an access token can be obtained from the signed

    request that is POST ed to your app but this will only ever be true if the user has already authorized your app. If

    the user has not authorized your app yet, youll need to log them in using the OAuth 2.0 login flow as described inthe section above or by using the JavaScript SDK.

    If an access token exists in the signed request, it can be obtained using the FacebookCanvasHelper . An instance of

    this helper can be generated from the Facebook super service by using the getCanvasHelper() method.

    123456789

    1011121314151617181920212223

    24252627282930313233

    $fb= newFacebook\Facebook([/* . . . */]);$helper= $fb>getRedirectLoginHelper();try{ $accessToken= $helper>getAccessToken();} catch(Facebook\Exceptions\FacebookSDKException $e) { // There was an error communicating with Graph echo$e>getMessage(); exit;}if(isset($accessToken)) { // User authenticated your app! // Save the access token to a session and redirect $_SESSION['facebook_access_token'] = (string) $accessToken; // Log them into your web framework here . . . echo'Successfully logged in!'; exit;} elseif($helper>getError()) { // The user denied the request // You could log this data . . . var_dump($helper>getError()); var_dump($helper>getErrorCode());

    var_dump($helper>getErrorReason()); var_dump($helper>getErrorDescription()); // You could display a message to the user // being all like, "What? You don't like me?" exit;}// If they've gotten this far, they shouldn't be herehttp_response_code(400);exit;

  • 7/25/2019 Facebook PHP SDK v5

    7/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 7/26

    Page tabs and canvas apps are virtually identical but there are a few important differences. One added feature of

    a Page tab app is that the signed request will contain additional data about the parent page.

    There is a FacebookPageTabHelper that helps you interface with the components available to your Page tab. The

    page tab helper extends from the FacebookCanvasHelper so all the methods are adopted as well. And there are a

    few additional methods you get when using the FacebookPageTabHelper .

    Access Tokens from the JavaScript SDK

    The JavaScript SDK (https://developers.facebook.com/docs/javascript)has a really great UI for Facebook Login.

    It is a better user experience to use the JavaScript SDK to authenticate a user into your app than it is to use the

    PHP SDKs OAuth 2.0 flow with the FacebookRedirectLoginHelper .

    I know what youre thinking: Once I log a user in with the JavaScript SDK, how does my PHP script access the

    access token?

    By default the JavaScript SDK wont store any info about the user on your domain unless you explicitly tell it to. So

    in your FB.init() method (https://developers.facebook.com/docs/javascript/reference/FB.init/v2.3), youll

    need to enable the cookie option with {cookie:true} . This tells the JavaScript SDK to set a cookie on your

    domain that contains a signed request with information about the authenticated user.

    If an access token exists in the signed request that was set by the JavaScript SDK, it can be obtained using the

    FacebookJavaScriptHelper . An instance of this helper can be generated from the Facebook super service by using

    the getJavaScriptHelper() method.

    123456789

    1011

    12131415

    $fb= newFacebook\Facebook([/* . . . */]);$helper= $fb>getCanvasHelper();try{ $accessToken= $helper>getAccessToken();} catch(Facebook\Exceptions\FacebookSDKException $e) { // There was an error communicating with Graph // Or there was a problem validating the signed request echo$e>getMessage(); exit;}

    if($accessToken) { echo'Successfully logged in!';}

    123456789

    10111213

    14

    $fb= newFacebook\Facebook([/* . . . */]);$helper= $fb>getPageTabHelper();// Returns info about the parent page$pageData= $helper>getPageData();// A boolean of whether or not the// authenticated user is an admin// of the parent page$isAdmin= $helper>isAdmin();// The ID of the parent page

    $pageId= $helper>getPageId();

    12345

    FB.init({ appId : '{appid}', cookie : true, version : 'v2.3'});

    https://developers.facebook.com/docs/javascript/reference/FB.init/v2.3https://developers.facebook.com/docs/javascript
  • 7/25/2019 Facebook PHP SDK v5

    8/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 8/26

    About Access Tokens

    Access tokens are represented with an AccessToken entity. There are two ways to get the access token as a

    plain-old string.

    Having AccessToken entities gives us some more power when handling access tokens. For example, you can do a

    strict check to ensure that you definitely have an access token.

    You can also typehint function & method arguments that take an access token.

    There are also a number of handy methods on the AccessToken entity.

    Getting a Long-Lived (Extended) Access Token

    123456789

    1011

    12131415

    $fb= newFacebook\Facebook([/* . . . */]);$helper= $fb>getJavaScriptHelper();try{ $accessToken= $helper>getAccessToken();} catch(Facebook\Exceptions\FacebookSDKException $e) { // There was an error communicating with Graph // Or there was a problem validating the signed request echo$e>getMessage(); exit;}

    if($accessToken) { echo'Successfully logged in!';}

    123

    // These statements are equivalentecho(string) $accessToken;echo$accessToken>getValue();

    123

    if($accessTokeninstanceofFacebook\Authentication\AccessToken) { // Logged in.}

    123

    function(Facebook\Authentication\AccessToken $token) { // . . .}

    1

    23456789

    1011121314151617

    // Returns expiration as a DateTime entity

    $expiresAt= $accessToken>getExpiresAt();// Returns boolean$expired= $accessToken>isExpired();// Returns boolean$isLong= $accessToken>isLongLived();// Returns boolean$isAppToken= $accessToken>isAppAccessToken();// Returns the app secret proof as a string// This is used to sign requests to the Graph API// All requests made using the PHP SDK are// signed automatically for you$proof= $accessToken>getAppSecretProof( '{appsecret}');

  • 7/25/2019 Facebook PHP SDK v5

    9/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 9/26

    You can exchange a short-lived access token for a long-lived access token by generating an instance of the

    OAuth2Client() by using the getOAuth2Client() method on the Facebook super service.

    The OAuth2Client has a number of cool features like a debugToken() method that returns an

    AccessTokenMetadata entity with information about the access token to be used to validate against or debug.

    Application Permissions

    Its important that you think about what permissions youll need for your site before your users start using it. If you

    ever need to update them, users will have to re-authorize the app. Once you have a list of permissions, pass them

    to the getLoginURL method as an array:

    Facebook PHP SDK v4: Application Permissions

    If no permissions are provided, itll use Facebooks default public_profile permission. Heres a list of some

    common permissions:

    User

    permissionDescription

    Gives access to a subset of a persons public profile:

    123456

    789

    10

    $cilent= $fb>getOAuth2Client();try{ // Returns a longlived access token $accessToken= $cilent>getLongLivedAccessToken('{shortlivedtoken}');} catch(Facebook\Exceptions\FacebookSDKException $e) {

    // There was an error communicating with Graph echo$e>getMessage(); exit;}

    1234

    56789

    1011121314151617

    try{ $metaData= $cilent>debugToken('{accesstoken}');} catch(Facebook\Exceptions\FacebookSDKException $e) { // There was an error communicating with Graph

    echo$e>getMessage(); exit;}var_dump($metaData>getAppId());var_dump($metaData>getApplication());var_dump($metaData>isError());var_dump($metaData>getIssuedAt());// These all throw a FacebookSDKException$metaData>validateAppId( '{appid}');$metaData>validateUserId('{userid}');$metaData>validateExpiration();

    12345

    6789

    // Requested permissions optional$permissions= array( 'email', 'user_location', 'user_birthday'

    );// Get login URL$loginUrl= $helper>getLoginUrl($callback, $permissions);

  • 7/25/2019 Facebook PHP SDK v5

    10/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 10/26

    public_profile

    id

    name

    first_name

    last_name

    link

    gender

    locale

    age_range

    emailProvides access to the users primary email address in the email property on the user

    object.

    user_location Provides access to the users current city as the location property.

    user_birthday Provides access to the birthday.

    See the full list of available permissions (https://developers.facebook.com/docs/facebook-

    login/permissions/v2.0).

    Making Requests to the Graph API

    Once weve successfully logged in, we can begin making calls to the Graph API. The Graph API only supports the

    GET , POST and DELETE HTTP verbs. In v5, each of these verbs get their own corresponding method.

    If you dont want to have to send in '{accesstoken}' with every method call in v5, you can use the

    setDefaultAccessToken() method.

    The responses from the get() , post() & delete() methods return a FacebookResponse entity which is an object

    that represents an HTTP response from the Graph API.

    If all you want is a plain-old PHP array, you call the getDecodedBody() method on the FacebookResponse entity.

    12345678

    // GET request.$res= $fb>get('/me', '{accesstoken}' );// POST request.$res= $fb>post('/me/feed', $data, '{accesstoken}');// DELETE request.$res= $fb>delete('/123', $data, '{accesstoken}');

    123456

    $fb>setDefaultAccessToken( '{accesstoken}');# These will fall back to the defaultaccess token$res= $fb>get('/me');$res= $fb>post('/me/feed', $data);$res= $fb>delete('/123', $data);

    123

    // Response example.$res= $fb>get('/me');

    https://developers.facebook.com/docs/facebook-login/permissions/v2.0
  • 7/25/2019 Facebook PHP SDK v5

    11/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 11/26

    v5 can return the response data as a collection which can be really handy for performing actions on the response

    data.

    I wont get into too much detail with this, but the collections come in the form of GraphObjects and they can be

    obtained from the FacebookResponse entity using the getGraphObject() method.

    Handling Exceptions

    Sometimes the Graph API will return an error response. Sad face.In v5 a FacebookSDKException will be thrown if

    something goes wrong.

    There are more than just the base FacebookSDKException in v5 but I wont get into too much detail on the other

    types of exceptions. But know that the other exceptions extend from FacebookSDKException so you can be

    assured that whenever you catch FacebookSDKException , youre catching any of the extended exceptions as well.

    The FacebookSDKException is not limited to error responses from the Graph API. There are other instances when a

    base FacebookSDKException might be thrown in v5. Some examples include a signed request fails to validate, a file

    you want to upload cannot be found and so on.

    Facebook PHP SDK v4: Making Requests to the Graph API

    Facebook PHP SDK Examples

    Once you have a session, youll have access to the all-powerful API. You can use this API to retrieve users

    information, implement one-click registrations, logins, publish to profiles and more.

    Check out the examples below to get the Facebook SDK PHP library (v5.x)

    (https://developers.facebook.com/docs/reference/php/4.0.0)integrated quickly.

    456

    var_dump($res>getDecodedBody());// array(10) { ...

    123456789

    101112

    // Response example.$res= $fb>get('/me');$node= $res>getGraphObject();var_dump($node>getProperty('id'));// string(3) "123"// Functionalstyle!$node>map(function($value, $key) { // . . .});

    1234

    56

    try{ $res= $fb>get('/123');} catch(Facebook\Exceptions\FacebookSDKException $e) { echo$e>getMessage();

    exit;}

    https://developers.facebook.com/docs/reference/php/4.0.0
  • 7/25/2019 Facebook PHP SDK v5

    12/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 12/26

    One-Click Registration & Login

    Heres a basic example of how to use the Facebook PHP SDK v5 to enable one-click registrations and logins:

    Facebook PHP SDK v4: One-Click Registration & Login

    Log a User Out

    Its usually not a common practice to make use of the log out feature within the PHP SDK as this will log a user out

    of Facebook completely, not just your app. So unless youve got a specific edge case where you need to do this,

    its not recommended that you use it.

    That said, you can log a user out of Facebook in v5 using the getLogoutUrl() method on the

    FacebookRedirectLoginHelper .

    12345

    6789

    101112131415161718192021

    22232425262728293031323334353637383940414243444546474849505152

    53

  • 7/25/2019 Facebook PHP SDK v5

    13/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 13/26

    Where http://example.com is the URL the user should be redirected to after logging out.

    Retrieve Users Profile Information

    Facebook PHP SDK v4: Retrieve Users Profile Information

    Note:Fields that arent set to public must be be requested with an access token with the relevant

    Extended Profile permissions.

    For more information and a full list of possible fields returned, see

    https://developers.facebook.com/docs/graph-api/reference/v2.0/user

    (https://developers.facebook.com/docs/graph-api/reference/v2.0/user).

    Get Users Profile Picture

    Facebook PHP SDK v4: Get Users Profile Picture

    You can use a list of modifiers to specify the type (e.g. square, small, normal, large), size and whether to send as a

    JSON array or output as an image. See a list of the available modifiers below:

    Modifiers

    Name Description Type

    redirect

    The picture edge is a special case, as when requested, it

    will by default return the picture itself and not a JSON

    response. To return a JSON response, you need to set

    redirect=false as a request attribute. This is how to return

    bool

    123456

    $fb= newFacebook\Facebook([/* */]);$helper= $fb>getRedirectLoginHelper();$logoutUrl= $helper>getLogoutUrl('{accesstoken}', 'http://example.com (http://example.cecho'Logout of Facebook!';

    123456789

  • 7/25/2019 Facebook PHP SDK v5

    14/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 14/26

    .

    type You use this to get a pre-specified size of picture. enum{square,small,normal,large}

    height Restrict the picture height to this size in pixels. int

    width

    Restrict the picture width to this size in pixels. When height

    and width are both used, the image will be scaled as close

    to the dimensions as possible and then cropped down.

    int

    For more information and a full list of possible fields returned, see

    https://developers.facebook.com/docs/graph-api/reference/v2.3/user/picture

    (https://developers.facebook.com/docs/graph-api/reference/v2.3/user/picture)

    Publish to Users Timeline

    A user access token with publish_actions permission can be used to publish new posts.

    Facebook PHP SDK v4: Publish to Users Timeline

    For more information and a full list of additional parameters, see https://developers.facebook.com/docs/graph-

    api/reference/v2.3/user/feed (https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed)

    Retrieve Users Timeline

    A user access token with read_stream permission is required and only posts whose authors have also granted

    read_stream permission to the app will be shown.

    Facebook PHP SDK v4: Retrieve Users Timeline

    There are other edges which provide filtered versions of this edge:

    /{userid}/links shows only the links that were published by this person.

    /{userid}/posts shows only the posts that were published by this person.

    /{userid}/statuses shows only the status update posts that were published by this person.

    /{userid}/tagged shows only the posts that this person was tagged in.

    123456789

    10

  • 7/25/2019 Facebook PHP SDK v5

    15/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 15/26

    All of these derivative edges share the exact same reading structure, however /feed should be used for all

    publishing purposes.

    For more information, see https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed

    (https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed).

    Upload a File

    Upload a Video

    If youre uploading a video, that requires using the videoToUpload() method.

    For more information, see https://developers.facebook.com/docs/graph-api/reference/video(https://developers.facebook.com/docs/graph-api/reference/video).

    Additional Resources

    For more information on the Graph API, see https://developers.facebook.com/docs/graph-api/reference/v2.0/

    (https://developers.facebook.com/docs/graph-api/reference/v2.0/). This has a full list of root nodes of the

    Graph API, with links to the reference docs for each.

    RELATED: Facebook SDK PHP v4 & CodeIgniter (http://www.benmarshall.me/facebook-sdk-php-v4-

    codeigniter/)

    If you have any questions or problems, post your comments below. Ill try to keep this post updated with the

    latest information and add more API calls later on. Have fun coding!

    12345678

    $fb= newFacebook\Facebook([/* . . . */]);$data= [ 'source'=> $fb>fileToUpload('/path/to/photo.jpg'), 'message'=> 'My file!', ];$response= $fb>post('/me/photos', $data, '{accesstoken}');

    123456789

    $fb= newFacebook\Facebook([/* . . . */]);$data= [ 'source' => $fb>videoToUpload('/path/to/video.mp4' ), 'title' => 'My video', 'description'=> 'My amazing video!', ];$response= $fb>post('/me/videos', $data, '{accesstoken}');

    (https://benmarshall.me/facebook-php-sdk/?share=google-plus-1&nb=1) (https://benmarshall.me/facebook-php-sdk/?share=twitter&nb=1)

    (https://benmarshall.me/facebook-php-sdk/?share=facebook&nb=1)6

    (https://benmarshall.me/facebook-php-sdk/?share=linkedin&nb=1)

    (https://benmarshall.me/facebook-php-sdk/?share=reddit&nb=1) More

    Related

    https://benmarshall.me/facebook-php-sdk/?share=reddit&nb=1https://benmarshall.me/facebook-php-sdk/?share=linkedin&nb=1https://benmarshall.me/facebook-php-sdk/?share=facebook&nb=1https://benmarshall.me/facebook-php-sdk/?share=twitter&nb=1https://benmarshall.me/facebook-php-sdk/?share=google-plus-1&nb=1http://www.benmarshall.me/facebook-sdk-php-v4-codeigniter/https://developers.facebook.com/docs/graph-api/reference/v2.0/https://developers.facebook.com/docs/graph-api/reference/videohttps://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed
  • 7/25/2019 Facebook PHP SDK v5

    16/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 16/26

    41 CommentsYou can post comments in this post.

    Facebook SDK PHP v4 & CodeIgniter

    (https://benmarshall.me/facebook-sdk-

    php-v4-codeigniter/)

    Google Analytics API Tutorial with PHP for

    Dummies

    (https://benmarshall.me/google-

    analytics-api-tutorial/)

    Mozscape API PHP Class

    (https://benmarshall.me/mozscape-api-

    php-class/)

    Tags: APIs (https://benmarshall.me/tag/apis/), Facebook (https://benmarshall.me/tag/facebook/), Facebook API(https://benmarshall.me/tag/facebook-api/), Facebook SDK PHP (https://benmarshall.me/tag/facebook-sdk-php/),

    Facebook SDK PHP v4 (https://benmarshall.me/tag/facebook-sdk-php-v4/), Facebook SDK PHP v5

    (https://benmarshall.me/tag/facebook-sdk-php-v5/)

    uide!)

    (https://plus.google.com/share?url=https://benmarshall.me/facebook-php-sdk/)

    k%22%3EFacebook+SDK+PHP+library+%28v4%29%3C%2Fa%3E+with+easy+to+follow+examples+to+get+your+site+integrated+quickly.)

    About This Author

    Red Bull Addict, Self-Proclaimed Grill Master, Entrepreneur, Workaholic, Front End Engineer, SEO/SM Strategist, Web

    Developer, Blogger

    [] Facebook SDK PHP v4 a complete guide! by Ben Marshall []

    Facebook SDK PHP V4 & CodeIgniter Working Together! (Http://Www.Benmarshall.Me/Facebook-Sdk-Php-V4-

    Codeigniter/) 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=317#respond)

    Using version 4.0.9. Above example dont works to me.

    Niko Jojo (Https://Www.Facebook.Com/Fb.Nikojojo) 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?

    replytocom=419#respond)

    Can you be more specific? Just tested the code with the latest and provided downloadable demo files for an

    example. Try those out and see if it works for you.

    https://benmarshall.me/facebook-php-sdk/?replytocom=419#respondhttps://www.facebook.com/fb.Nikojojohttps://benmarshall.me/facebook-php-sdk/?replytocom=317#respondhttp://www.benmarshall.me/facebook-sdk-php-v4-codeigniter/http://www.tumblr.com/share/link?url=https%3A%2F%2Fbenmarshall.me%2Ffacebook-php-sdk%2F&name=Facebook+PHP+SDK+v5.x+%284.1%29+%E2%80%94+a+complete+guide%21&description=Facebook+SDK+PHP+is+a+powerful+library+allowing+you+to+retrieve+user+information%2C+publish+to+profiles%2C+enable+Facebook+registrations%2C+logins+and+more.+Learn+how+to+easily+implement+the+%3Ca+href%3D%22https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Freference%2Fphp%2F4.0.0%22+target%3D%22_blank%22%3EFacebook+SDK+PHP+library+%28v4%29%3C%2Fa%3E+with+easy+to+follow+examples+to+get+your+site+integrated+quickly.https://plus.google.com/share?url=https://benmarshall.me/facebook-php-sdk/http://linkedin.com/shareArticle?mini=true&url=https://benmarshall.me/facebook-php-sdk/&title=Facebook%20PHP%20SDK%20v5.x%20(4.1)%20%E2%80%94%20a%20complete%20guide!https://benmarshall.me/tag/facebook-sdk-php-v5/https://benmarshall.me/tag/facebook-sdk-php-v4/https://benmarshall.me/tag/facebook-sdk-php/https://benmarshall.me/tag/facebook-api/https://benmarshall.me/tag/facebook/https://benmarshall.me/tag/apis/https://benmarshall.me/mozscape-api-php-class/https://benmarshall.me/google-analytics-api-tutorial/https://benmarshall.me/facebook-sdk-php-v4-codeigniter/
  • 7/25/2019 Facebook PHP SDK v5

    17/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 17/26

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1634#respond)

    Hi, there your code where you get token:

    https://gist.github.com/bmarshall511/cbe0a6faab9fc0505638#file-1457-1-php-L66

    (https://gist.github.com/bmarshall511/cbe0a6faab9fc0505638#file-1457-1-php-L66)

    I tried the same way, but there are token=app_id|secret_id, so this is not that secret token

    that generate facebook to create FacebookSession and use their API, and i get exception:

    An active access token must be used to query information about the current user.

    I wanna store access token, but cant understand how to get it.

    Meteor (Http://Facebook.Com) 1 year ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1641#respond)

    Problem solved. I was wrong with trying to get token without facebook

    response

    Meteor (Http://Facebook.Com) 1 year ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1642#respond)

    there is problem with session, after 2 hours it gets destroyed , and i dont know how to renew it, it will then will throw error about

    authorization shit all the time, refresh wont help, only deleting cookies helped

    btw i tried the example with codeigniter, not without

    Gaucan (Http://Gravatar.Com/Gaucan) 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?

    replytocom=440#respond)

    Session are handled by the server. If you want the session to last longer, see

    http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes

    (http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes).

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1635#respond)

    https://benmarshall.me/facebook-php-sdk/?replytocom=1635#respondhttp://www.benmarshall.me/http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minuteshttps://benmarshall.me/facebook-php-sdk/?replytocom=440#respondhttp://gravatar.com/gaucanhttps://benmarshall.me/facebook-php-sdk/?replytocom=1642#respondhttp://facebook.com/https://benmarshall.me/facebook-php-sdk/?replytocom=1641#respondhttp://facebook.com/https://gist.github.com/bmarshall511/cbe0a6faab9fc0505638#file-1457-1-php-L66https://benmarshall.me/facebook-php-sdk/?replytocom=1634#respondhttp://www.benmarshall.me/
  • 7/25/2019 Facebook PHP SDK v5

    18/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 18/26

    Thanks,your code is working fine.

    Rohini 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=444#respond)

    PHP message: PHP Fatal error: Cannot use Facebook\FacebookCurl as FacebookCurl because the name is already in use

    RahulG (Http://Gravatar.Com/Papa10) 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?

    replytocom=765#respond)

    Looks like youre redefining a namespace. Download the demo files to see an example.

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1633#respond)

    can you tell me how to configure in yii framework

    Rahulg FDEV 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=771#respond)

    This tutorial has really helped me a lot as I get started with the Facebook SDK for PHP. Many thanks for posting it!

    I was wondering what is the purpose of the code on line 76? At this point in your code it seems that you have already verified a

    session. It doesnt seem necessary to call FacebookSession() again or am I missing something?

    Thanks again for a great tutorial

    Matt 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=862#respond)

    https://benmarshall.me/facebook-php-sdk/?replytocom=862#respondhttps://benmarshall.me/facebook-php-sdk/?replytocom=771#respondhttps://benmarshall.me/facebook-php-sdk/?replytocom=1633#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=765#respondhttp://gravatar.com/papa10https://benmarshall.me/facebook-php-sdk/?replytocom=444#respond
  • 7/25/2019 Facebook PHP SDK v5

    19/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 19/26

    That code was a little outdated. Just updated the post and example code with the latest. Should make more

    sense now.

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1636#respond)

    i follow ur code but it didnt generates the access token can u tell me where did i made mistake

    Nisha 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1562#respond)

    Gonna need a little more info than that to help. Can you provide me the code or error youre seeing?

    Ben Marshall (Http://Www.Benmarshall.Me) 10 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1761#respond)

    Can you please explain , where do we have to put (starting with session_start() ) those coding in our hosting file. do we have to

    add with our index.php?

    Keeran (Http://Www.Findaproperty.Lk) 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?

    replytocom=1623#respond)

    It just depends on the app youre building. The important part is it needs to included before any output is

    sent.

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1632#respond)

    https://benmarshall.me/facebook-php-sdk/?replytocom=1632#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1623#respondhttp://www.findaproperty.lk/https://benmarshall.me/facebook-php-sdk/?replytocom=1761#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1562#respondhttps://benmarshall.me/facebook-php-sdk/?replytocom=1636#respondhttp://www.benmarshall.me/
  • 7/25/2019 Facebook PHP SDK v5

    20/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 20/26

    Thanks for the code, it works perfectly!

    But Im stuck with this: I try to change the request url, it returns data nicely in the Facebook Graph API Explorer Tool, but in my

    site on localhost (appId and appSecret are correct) just returns a null array! Do you have any idea?

    Thinh Nguyen (Http://Thinhnc.Com) 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?

    replytocom=1626#respond)

    Make sure you set the apps App Domainssetting to the domain youll be using the app on (e.g.

    localhost.dev). Otherwise, you wont have access to the API.

    its a good idea to create a Test App (https://developers.facebook.com/docs/apps/test-apps/).

    This is especially useful when working on a local environment. Test Apps have their own App ID,

    App Secret and settings. This allows you to set the App Domainssetting to your local environment

    URL without affecting the production version of your app.

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1631#respond)

    I tried your code, but every time I refresh the canvas page it asks me to login again. If older version of SDK, the user needed to

    add the app just once. Do you have any fix for that?

    Tony (Http://Google.Com) 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1628#respond)

    If your app is on Facebook Canvas, use the getSession() method on FacebookCanvasLoginHelper

    (https://developers.facebook.com/docs/php/FacebookCanvasLoginHelper) to get a FacebookSession for

    the user.

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1630#respond)

    https://benmarshall.me/facebook-php-sdk/?replytocom=1630#respondhttp://www.benmarshall.me/https://developers.facebook.com/docs/php/FacebookCanvasLoginHelperhttps://benmarshall.me/facebook-php-sdk/?replytocom=1628#respondhttp://google.com/https://benmarshall.me/facebook-php-sdk/?replytocom=1631#respondhttp://www.benmarshall.me/https://developers.facebook.com/docs/apps/test-apps/https://benmarshall.me/facebook-php-sdk/?replytocom=1626#respondhttp://thinhnc.com/
  • 7/25/2019 Facebook PHP SDK v5

    21/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 21/26

    Really awesome content, it helped me allot, thanks!

    Hirdesh 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1661#respond)

    Hi,

    I copied your code from demo, and created fb applicaiton and security token and app id were taken, But i m getting this error

    Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or ( in

    C:wampwwwfacebookindex.php on line 23

    Nirksa 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1673#respond)

    Sounds like youve got some malformed code. Take a look at the example and compare to what youve got.

    Ben Marshall (Http://Www.Benmarshall.Me) 10 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1762#respond)

    Im new at developing Facebook. Im trying to use your example on my server but it back with Parse error for use

    FacebookFacebookRequest; why?

    Tuareg 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1681#respond)

    Can I see your code?

    Ben Marshall (Http://Www.Benmarshall.Me) 1 year ago Reply (https://benmarshall.me/facebook-

    php-sdk/?replytocom=1697#respond)

    https://benmarshall.me/facebook-php-sdk/?replytocom=1697#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1681#respondhttps://benmarshall.me/facebook-php-sdk/?replytocom=1762#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1673#respondhttps://benmarshall.me/facebook-php-sdk/?replytocom=1661#respond
  • 7/25/2019 Facebook PHP SDK v5

    22/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 22/26

    thanks buddy, this work for me.

    but, how to get token like on https://developers.facebook.com/docs/facebook-login/access-tokens/

    (https://developers.facebook.com/docs/facebook-login/access-tokens/)???

    i try but have many error.

    actually about, Long-Lived Tokens.

    thanks

    Miler 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1704#respond)

    Ive updated the post to provide more details that should answer your question. Let me know if you run into

    any problems.

    Ben Marshall (Http://Www.Benmarshall.Me) 10 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1750#respond)

    Hello, i am a bit confused about some things if you could explain me i would appreciated a lot.

    I am trying to create a facebook app with both platforms of Facebook Canvas and Page tab.

    I am currently working at Page tab. I want to use the php sdk to create the Auth dialog.

    Lets say my file for the facebook session and stuff is hosted at demo.myhost.gr/app1/fbmain.php

    So my App Domains is demo.myhost.gr

    If i have understood correctly i am supposed to use the FacebookRedirectLoginHelper($redirect_uri)

    and $redirect_uri must be https://demo.myhost.gr/app1/fbmain.php (https://demo.myhost.gr/app1/fbmain.php)

    This means that when the user will click at $loginUrl he will see the auth dialog and then he will leave Facebook and go at

    $redirect_uri.

    So i must save the session and have a redirection back to my Facebook Page and app.

    Am i correct till here?

    Also the Log out (' . $logoutURL . ') it gives me a white page inside the Faceboook Tab, with no auth dialog.

    Dimitris Sarmis 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1705#respond)

    Yes, thats basically it. Ive updated the post with more details on the whole process which should help. Let

    me know if you still run into troubles.

    Ben Marshall (Http://Www.Benmarshall.Me) 10 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1749#respond)

    https://benmarshall.me/facebook-php-sdk/?replytocom=1749#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1705#respondhttps://benmarshall.me/facebook-php-sdk/'%20.%20$logoutURL%20.%20'https://demo.myhost.gr/app1/fbmain.phphttps://benmarshall.me/facebook-php-sdk/?replytocom=1750#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1704#respondhttps://developers.facebook.com/docs/facebook-login/access-tokens/
  • 7/25/2019 Facebook PHP SDK v5

    23/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 23/26

    This is GREAT post man ^. Saved my day. However is there any way to get cleaner url after login. Mine is little messy with

    code and state parameter.

    Boshigt 1 year ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1706#respond)

    for developing facebook can i using localhost for testing?

    Dany 12 months ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1722#respond)

    Yup.

    Ben Marshall (Http://Www.Benmarshall.Me) 12 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1723#respond)

    oke thank you, but when i upload to hosting and i try access i get error liki this

    Fatal error: Cannot access protected property

    Facebook\FacebookSDKException::$message in

    /home/u648397598/public_html/facebook/index.php on line 62can you fix this problem?

    Dany (Http://Wijayantodeni.Com) 11 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1725#respond)

    I used this article to get me going and everything is working like Id expect. Was wondering if anyone here knows how to get it

    to work with WordPress?

    Everything works like I want except I lose the session when I navigate the site after I login.

    I tried to use the init action hook but get this error from the SDK Session not active, could not load state.

    https://benmarshall.me/facebook-php-sdk/?replytocom=1725#respondhttp://wijayantodeni.com/https://benmarshall.me/facebook-php-sdk/?replytocom=1723#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1722#respondhttps://benmarshall.me/facebook-php-sdk/?replytocom=1706#respond
  • 7/25/2019 Facebook PHP SDK v5

    24/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 24/26

    Any help/direction would be sweet! andthanks for the article.

    Thanks,

    Matt

    Matt 10 months ago Reply (https://benmarshall.me/facebook-php-sdk/?replytocom=1753#respond)

    Take a look at https://wordpress.org/support/topic/using-session-in-wordpress

    (https://wordpress.org/support/topic/using-session-in-wordpress) Should help with the session issue in

    WordPress.

    Ben Marshall (Http://Www.Benmarshall.Me) 10 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1754#respond)

    Ive followed your steps, looked at the demo files, etcbut I keep running into a problem where my session isnt ever set, so all

    I get is the Login URL every time. Could this have something to do with the new changes Facebook is making ?

    I feel like I had this exact problem before the last time I set up FB integration last year, and no one has examples that work like

    theyre supposed to. Just to be clear, the redirect_uri should take me back this same file location, right?

    Jason Seabaugh (Http://Www.Jasonseabaugh.Com) 8 months ago Reply (https://benmarshall.me/facebook-php-

    sdk/?replytocom=1787#respond)

    Ive updated the tutorial with the new v5 (4.1) features. Also provided some more example that should help

    you out. Let me know if you still run into problems.

    Ben Marshall (Http://Www.Benmarshall.Me) 6 months ago Reply(https://benmarshall.me/facebook-php-sdk/?replytocom=1812#respond)

    Awesome content.

    Joel (Http://Www.Benmarshall.Me/) 7 months ago Reply (https://benmarshall.me/facebook-php-sdk/?

    replytocom=1800#respond)

    https://benmarshall.me/facebook-php-sdk/?replytocom=1800#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1812#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1787#respondhttp://www.jasonseabaugh.com/https://benmarshall.me/facebook-php-sdk/?replytocom=1754#respondhttp://www.benmarshall.me/https://wordpress.org/support/topic/using-session-in-wordpresshttps://benmarshall.me/facebook-php-sdk/?replytocom=1753#respond
  • 7/25/2019 Facebook PHP SDK v5

    25/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    https://benmarshall.me/facebook-php-sdk/ 25/26

    Leave a Reply

    Enter your comment here...

    I am getting this error.

    Does this require me to get permissions from facebook for each action mentioned in the Status and Review section of my app.

    Fatal error: Uncaught exception Facebook\FacebookPermissionException with message (#200) The user hasnt authorized the

    application to perform this action

    Manik (Http://Www.Potentspace.Com) 7 months ago Reply (https://benmarshall.me/facebook-php-sdk/?

    replytocom=1806#respond)

    Looks like the access token may be incorrect or your session data isnt getting passed over. Can I see thecode youre using?

    Ben Marshall (Http://Www.Benmarshall.Me) 6 months ago Reply

    (https://benmarshall.me/facebook-php-sdk/?replytocom=1813#respond)

    Ben Marshall

    Red Bull Addict | Self-Proclaimed Grill Master | Entrepreneur | Workaholic | Front End Engineer | SEO/SM Strategist | Web Developer |

    Blogger

    LATEST TWITTER FEED

    The #Drupal (https://twitter.com/search?q=%23Drupal&src=hash)#TyntModule (https://twitter.com/search?

    q=%23TyntModule&src=hash) has now been ported & released for #D8 (https://twitter.com/search?q=%23D8&src=hash)! via

    @bmarshall0511 (https://twitter.com/bmarshall0511)#Drupal8 (https://twitter.com/search?q=%23Drupal8&src=hash)#drupaldev

    (https://twitter.com/search?q=%23drupaldev&src=hash)#Celebr8D8 (https://twitter.com/search?q=%23Celebr8D8&src=hash)

    https://t.co/HLbZYcnP8x (https://t.co/HLbZYcnP8x)

    November 19, 2015

    Need an array of user roles in your #Drupal8 (https://twitter.com/search?q=%23Drupal8&src=hash)site? Say hello to

    user_role_names(). via @bmarshall0511 (https://twitter.com/bmarshall0511)#drupal (https://twitter.com/search?

    q=%23drupal&src=hash) #d8 (https://twitter.com/search?q=%23d8&src=hash)https://t.co/UlKj2lAJxc (https://t.co/UlKj2lAJxc)

    November 19, 2015

    https://t.co/UlKj2lAJxchttps://twitter.com/search?q=%23d8&src=hashhttps://twitter.com/search?q=%23drupal&src=hashhttps://twitter.com/bmarshall0511https://twitter.com/search?q=%23Drupal8&src=hashhttps://t.co/HLbZYcnP8xhttps://twitter.com/search?q=%23Celebr8D8&src=hashhttps://twitter.com/search?q=%23drupaldev&src=hashhttps://twitter.com/search?q=%23Drupal8&src=hashhttps://twitter.com/bmarshall0511https://twitter.com/search?q=%23D8&src=hashhttps://twitter.com/search?q=%23TyntModule&src=hashhttps://twitter.com/search?q=%23Drupal&src=hashhttps://benmarshall.me/facebook-php-sdk/?replytocom=1813#respondhttp://www.benmarshall.me/https://benmarshall.me/facebook-php-sdk/?replytocom=1806#respondhttp://www.potentspace.com/
  • 7/25/2019 Facebook PHP SDK v5

    26/26

    1/12/2015 Facebook PHP SDK v5.x a complete guide! | Ben Marshall

    2015. Ben Marshall (http://www.benmarshall.me). All rights reserved.

    http://www.benmarshall.me/