The importance of a professional presence on Facebook is constantly 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.
Facebook constantly changes their SDK and methods for communicating with Facebook. The script in this post supports the latest Facebook authentication changes that will be implemented i October 2012.
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 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:
- Download Facebook PHP SDK
- Register the Facebook application
- 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:
- Configuration where you insert your Facebook SDK information, and configeres which content to post on Facebook
- Facebook authentication and posting if a valid access token is avalilable
- Facebook authentication token download if no valid token is available (and then the script proceeds to step 2)
03 | require_once 'facebook_sdk/src/facebook.php' ; |
06 | $appid = '<insert app id>' ; |
07 | $appsecret = '<insert app secret' ; |
08 | $pageId = 'myoutlets' ; |
09 | $msg = 'Nice script for posting to Facebook from PHP program' ; |
10 | $title = '1webtutor.blogspot.com' ; |
12 | $desc = 'Learn how to build a script that automatically can post messages from a PHP script to Facebook' ; |
14 | $action_name = 'Go to 1Webtutor' ; |
17 | $facebook = new Facebook( array ( |
19 | 'secret' => $appsecret , |
23 | $user = $facebook ->getUser(); |
29 | $page_info = $facebook ->api( "/$pageId?fields=access_token" ); |
30 | if (! empty ( $page_info [ 'access_token' ])) { |
32 | 'access_token' => $page_info [ 'access_token' ], |
36 | 'description' => $desc , |
38 | 'actions' => json_encode( array ( 'name' => $action_name , 'link' => $action_link )) |
41 | $status = $facebook ->api( "/$pageId/feed" , "post" , $attachment ); |
43 | $status = 'No access token recieved' ; |
45 | } catch (FacebookApiException $e ) { |
51 | header( "Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}" ); |
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.
No comments:
Post a Comment
Confused? Feel free to ask
Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.
Note:
Please do not spam Spam comments will be deleted immediately upon my review.