Facebook Developers
DocumentationSupportBlogAppsLog In
  • Getting Started
  • Core Concepts
  • Advanced Topics
  • SDK Reference
    • JavaScript SDK
    • PHP SDK
    • iOS SDK
    • Android SDK
  • Tools
  • Downloads
    • Download the PHP SDK
  • Facebook Object
    • api
    • getAccessToken
    • getApiSecret
    • getAppId
    • getLoginStatusUrl
    • getLoginUrl
    • getLogoutUrl
    • getSignedRequest
    • getUser
    • setAccessToken
    • setApiSecret
    • setAppId
    • setFileUploadSupport
    • useFileUploadSupport
  • FacebookApiException
    • getResult
    • getType

PHP SDK Overview

SDK Reference › PHP SDK Overview

The PHP SDK provides a rich set of server-side functionality for accessing Facebook’s server-side API calls. These include all of the features of the Graph API, FQL, and the Deprecated REST API.

You can download the Facebook PHP SDK from GitHub here.

The PHP SDK is typically used to perform operations as an app administrator, but can also be used to perform operations on behalf of the current session user. By removing the need to manage access tokens manually, the PHP SDK greatly simplifies the process of authentication and authorizing users for your app.

You will need an app id to initialize the SDK, which you can obtain from the App Dashboard.

The PHP SDK can work in conjunction with the JavaScript SDK to provide seamless session management across both the client- and server-sides of an app.

Installing and Initializing

To install the PHP SDK, extract the downloaded files and copy the files from the src/ directory to a directory on the server where you will host your app, for example php-sdk. Then, just include php-sdk/facebook.php wherever you want to use the SDK. You use the SDK by instantiating a new Facebook object with, at a minimum, your app id and app secret:

  require_once("facebook.php");

  $config = array();
  $config[‘appId’] = 'YOUR_APP_ID';
  $config[‘secret’] = 'YOUR_APP_SECRET';
  $config[‘fileUpload’] = false; // optional

  $facebook = new Facebook($config);

The optional fileUpload parameter tells the SDK whether or not file uploads are enabled on your server. See the setFileUploadSupport documentation for more details.

Authentication & Authorization

The PHP SDK can be used to support registration and login to your site using the users’ Facebook account. On the server-side, the PHP SDK provides helper-methods to simply login, request permissions, and logout. This functionality is provided by the Facebook::getUser(), Facebook::getLoginUrl() and Facebook::getLogoutUrl() methods.

Integration with the JavaScript SDK

Used in conjunction with the JavaScript SDK, the PHP SDK can share user sessions seamlessly across the client and server. If a user is already logged in to their Facebook account, the JavaScript SDK will pick up the login session and send it to the server via a cookie, which the PHP SDK reads without any intervention on the developer's part. Example here.


  • Downloads
  • Facebook Object
  • FacebookApiException

Downloads

Download the PHP SDK

Link to download the PHP SDK (redirects to GitHub).


Facebook Object

api

Call a Graph API method, FQL Query, or (DEPRECATED) REST API using the PHP SDK.

getAccessToken

Get the current access token being used by the SDK.

getApiSecret

Get the App secret that the SDK is currently using.

getAppId

Get the App ID that the SDK is currently using.

getLoginStatusUrl

Returns a URL based on the user’s login status on Facebook.

getLoginUrl

Get a URL that the user can click to login, authorize the app, and get redirected back to the app.

getLogoutUrl

This method returns a URL that, when clicked by the user, will log them out of their Facebook session and then redirect them back to your application.

getSignedRequest

Get the current signed request being used by the SDK.

getUser

This method returns the Facebook User ID of the current user, or 0 if there is no logged-in user.

setAccessToken

Set the current access token being used by the SDK.

setApiSecret

Set the App secret that the SDK is currently using.

setAppId

Set the App ID that the SDK is currently using.

setFileUploadSupport

Set file upload support in the SDK.

useFileUploadSupport

Get whether file upload support has been enabled in the SDK.


FacebookApiException

getResult

Get the object that is the result of the error or exception returned by the server.

getType

Get the type for the error or exception, e.g. OAuthException.

Updated about 2 weeks ago
Facebook © 2012 · English (US)
AboutCareersPlatform PoliciesPrivacy Policy