Automatically post to Facebook from PHP script

[Updated 21-09-2012 to support the latest Facebook changes] The importance of a professional presence on Facebook is constaltly increasing.
However supporting and publishing content on Facebook as a parallel channel to a normal website means increased work for many companies.
However posting on facebook doesn’t have to be a manual task that requires extra work. This post guides you to a solution that can easily be integrated in a existing CMS system, and enable cross posting of content to Facebook.

This post guides you through the creation of a PHP based Facebook application that can automatically post messages and other types of content on your Facebook wall.

The final result of the script is a timeline post like the example below

The post features both a headline, a link to a website, a image, and action links.
The process of building the post to Facebook PHP script requires the following steps:
  1. Download Facebook PHP SDK
  2. Register the Facebook application
  3. Build the Post to Facebook script

Download the Facebook SDK

First step is to create a folder on your webserver for the Facebook application. In this example we’ll name the folder “facebook”
In the facebook folder create a subfolder names “facebook_sdk”
Download the Facebook PHP SDK from GitHub, unzip the files and upload the files to the “facebook_sdk” folder on your server.
The Facebook SDK files can be downloaded from here

Register the Facebook application

Facebook only allows registrated applications to interact with Facebook accounts and pages. The next step is therefore to register your application (even if it hasn’t been built yet) .
You can register a new Facebook application on this page
To register your Facebook application you need first need to provide a unique app name.

If everything is OK, you get to a registration conformation screen, where you get the two important informations:
  • App ID/API Key
  • App Secret

These informations are required for the application to be allowed to communicate with Facebook, but should also be kept as a secret.

Build the Post to Facebook script

Now it’s time to build the actual PHP script for posting to the Facebook timeline.
The script assumes that you have downloaded and unzipped the Facebook PHP SDK (with it’s original file structure and content) in a subfolder named “facebook_sdk”.
The script is divided into three parts:
  1. Configuration where you insert your Facebook SDK information, and configeres which content to post on Facebook
  2. Facebook authentication and posting if a valid access token is avalilable
  3. Facebook authentication token download if no valid token is available (and then the script proceeds to step 2)
 require_once 'facebook_sdk/src/facebook.php';
// configuration
 $appid = '<insert app id>';
 $appsecret = '<insert app secret';
 $pageId = 'myoutlets';
 $msg = 'Nice script for posting to Facebook from PHP program';
 $title = '';
 $uri = '';
 $desc = 'Learn how to build a script that automatically can post messages from a PHP script to Facebook';
 $pic = '';
 $action_name = 'Go to Earn In pkr';
 $action_link = '';
$facebook = new Facebook(array(
 'appId' => $appid,
 'secret' => $appsecret,
 'cookie' => false,
$user = $facebook->getUser();
// Contact Facebook and get token
 if ($user) {
 // you're logged in, and we'll get user acces token for posting on the wall
 try {
 $page_info = $facebook->api("/$pageId?fields=access_token");
 if (!empty($page_info['access_token'])) {
 $attachment = array(
 'access_token' => $page_info['access_token'],
 'message' => $msg,
 'name' => $title,
 'link' => $uri,
 'description' => $desc,
 'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
$status = $facebook->api("/$pageId/feed", "post", $attachment);
 } else {
 $status = 'No access token recieved';
 } catch (FacebookApiException $e) {
 $user = null;
 } else {
 // you're not logged in, the application will try to log in to get a access token
 header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
echo $status;

Next steps

This post guided you through the initial Facebook application registration, and basic script for automatically posting content to a Facebook wall.
Next step could be to integrate this script in your CMS system, so a new post is automatically added to Facebook every time you’re updating the content on your site.

Facebook application authentication is changing from october 2012, which means that it’s no longer possible to generate permanent or offline access tokens. Unfortunately this means that it’s getting more complicated to post to Facebook from a application – especially if the Facebook application is running through a cron job or similar.

Popular Posts