Post to Facebook Wall from Web Application using PHP
Facebook is the most popular social media network. Post to Facebook wall is useful when you want to post dynamic content to Facebook from the web application. In this tutorial, we will explain how to post activity on Facebook wall using PHP and Facebook API. This is a very simple example, you can just copy paste and change according to your requirement.
First, follow the below steps to create a Facebook App Account and generate App ID & App Secret Key.
- Go to the Facebook for Developers page and Login your Facebook Account.
- Right corner of the top navigation, click the My Apps link and select Create New App.
- Enter the Display Name and Contact Email.
- Click the Create App ID button.
- The new App will be created and redirected to the Facebook App Dashboard.
- You can Download the latest version of the SDK
Before started to implement the Post on Facebook wall using PHP, look files structure:
- post-to-facebook-wall-using-php-sdk
- facebook-php-graph-sdk
- index.php
- function.php
Create Facebook API Configuration file named configuration.php
- The configuration.php file is used to configure Facebook SDK and connect to Facebook Graph API.
- Access token must have the
publish_actions
permission to post on Facebook wall.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?php if(!session_id()){ session_start(); } // Include the Facebook SDK require_once __DIR__ . '/facebook-php-graph-sdk/autoload.php'; use Facebook\Facebook; use Facebook\Exceptions\FacebookResponseException; use Facebook\Exceptions\FacebookSDKException; /** * Configuration & setup Facebook API SDK **/ $appID = 'App ID'; //Facebook App ID $appSecretKey = 'App Secret Key'; //Facebook App Secret Key $redirectURL = 'https://techarise.com/demos/php/post-to-facebook-wall-using-php-sdk/'; //Callback URL $fbPermissions = array('publish_actions'); //Facebook permissions $fb = new Facebook(array( 'app_id' => $appID, 'app_secret' => $appSecretKey, 'default_graph_version' => 'v2.6', )); // Helper: Get redirect login $helper = $fb->getRedirectLoginHelper(); // Try to get access token try { if(isset($_SESSION['facebook_access_token'])){ $accessToken = $_SESSION['facebook_access_token']; }else{ $accessToken = $helper->getAccessToken(); } } catch(FacebookResponseException $e) { echo 'Graph returned an error: ' . $e->getMessage(); exit; } catch(FacebookSDKException $e) { echo 'Facebook SDK returned an error: ' . $e->getMessage(); exit; } ?> |
Submit Post to Facebook Wall file named index.php
- Include the configuration.php file to connect Facebook API & get the access token.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php // Include configuration file require_once 'configuration.php'; if(isset($accessToken)){ if(isset($_SESSION['facebook_access_token'])){ $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); }else{ // Access token in session $_SESSION['facebook_access_token'] = (string) $accessToken; // OAuth client handler helps to manage access tokens $oAuth2Client = $fb->getOAuth2Client(); // Exchanges a short-lived access token for a long-lived one $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']); $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken; // Set default access token $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); } //Facebook wall post content $message = 'Test message from Techarise'; $title = 'Post From Techarise'; $url = 'https://techarise.com/'; $description = 'Web Development Tutorials & Web Developer Resources - Techarise'; $img = 'https://techarise.com/wp-content/themes/techarise-banner.png'; $attachment = array( 'message' => $message, 'name' => $title, 'link' => $url, 'description' => $description, 'picture'=>$img, ); try{ // Wall Post to Facebook $fb->post('/me/feed', $attachment, $accessToken); echo 'Post published successfully.'; }catch(FacebookResponseException $e){ echo 'Graph error: ' . $e->getMessage(); exit; }catch(FacebookSDKException $e){ echo 'Facebook SDK error: ' . $e->getMessage(); exit; } }else{ // Facebook login URL $fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions); // Redirect to Facebook login page echo '<a href="'.$fbLoginURL.'"><img src="fb-btn.png" style="margin: -6px; width:50px; height:25px;"><strong style="font-size:20px;">Post to Facebook Wall</strong></a>'; } ?> |