Wednesday, March 27, 2013

Automatically post to Facebook from PHP script


 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:
  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)
01<pre>
02<?php
03 require_once 'facebook_sdk/src/facebook.php';
04
05// configuration
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';
15 $action_link 'http://1webtutor.blogspot.in';
16
17$facebook new Facebook(array(
18 'appId' => $appid,
19 'secret' => $appsecret,
20 'cookie' => false,
21 ));
22
23$user $facebook->getUser();
24
25// Contact Facebook and get token
26 if ($user) {
27 // you're logged in, and we'll get user acces token for posting on the wall
28 try {
29 $page_info $facebook->api("/$pageId?fields=access_token");
30 if (!empty($page_info['access_token'])) {
31 $attachment array(
32 'access_token' => $page_info['access_token'],
33 'message' => $msg,
34 'name' => $title,
35 'link' => $uri,
36 'description' => $desc,
37 'picture'=>$pic,
38 'actions' => json_encode(array('name' => $action_name,'link' =>$action_link))
39 );
40
41$status $facebook->api("/$pageId/feed""post"$attachment);
42 else {
43 $status 'No access token recieved';
44 }
45 } catch (FacebookApiException $e) {
46 error_log($e);
47 $user = null;
48 }
49 else {
50 // you're not logged in, the application will try to log in to get a access token
51 header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
52 }
53
54echo $status;
55 ?>

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.