Example – Post to Facebook Wall from Website in PHP

4

Facebook is the most popular social media and shares on the Facebook wall are the most used activity by its user. Facebook share option is a common and required feature for every web application. We can easily share the post to Facebook by manually or from the script. In this tutorial, you’ll learn how to post activity on Facebook wall from website using PHP and Facebook API.

Post to Facebook wall is useful when you want to post dynamic content to Facebook from the website. Here we’ll build a simple PHP script to publish Facebook post from website using Facebook PHP SDK. This functionality lets the user submit post (message, picture, link, text content) on their Facebook timeline from website using PHP SDK v5.0 and Facebook Graph API.

Facebook Apps Creation

To access Facebook API, App ID & App Secret need to be specified on Facebook API call. You need to create a Facebook App for generating App ID & App Secret. If you’re not already created a Facebook app, visit the link below to create and configure a Facebook App from the App Dashboard.

  • Creating a Facebook App and get App ID & App Secret.

After completing the Facebook App creation and configuration you’ll get the App ID and App secret. Copy this App ID and App Secret of your Facebook App for later use.

Facebook SDK for PHP v5.0

All Facebook PHP SDK files are included in the facebook-php-sdk/ directory, place the facebook-php-sdk/ folder into the root directory. You don’t need to download it separately, Facebook SDK v5 is included in our source code.

Facebook API Configuration (fbConfig.php)

The fbConfig.php file is used to configure Facebook SDK and connect to Facebook Graph API. Specify your Facebook App ID ($appId), App Secret ($appSecret), Callback URL ($redirectURL), and Permissions ($fbPermissions) to connect with Facebook API and working with SDK.

Note that: The access token must have the publish_actions permission to post on Facebook wall.

Note that: You’ll find the App ID and App Secret on your Facebook Apps settings page.

Submit Post to Facebook Wall (index.php)

Include the fbConfig.php file to connect Facebook API and get the access token.

If FB access token ($accessToken) is available, the following will happen.

  • Access token will be stored in the session that will be used for next time API calls.
  • Post message, name, link, description, and the picture will be submitted to Facebook wall.
  • Post submission status will be shown.

If FB access token ($accessToken) is not available, the Facebook Login URL will be generated and the user would be redirected to the FB login page.

 

Leave A Reply

Your email address will not be published.