I was assigned with the task to auto post to my work’s fan page from one of our content sytems. At first, I thought it to be a fairly trivial thing and wouldn’t take me that long but boy was I wrong. I started off by trying a bunch of the existing examples out there already that use curl to mimic someone logging in as they normally would, use regex to match the form variables and submit a status update using curl with the form variables. Facebook didn’t like this and promptly locked my account down for unauthorized access.
Next I moved on to the Facebook API.
The Facebook Graph API allows websites to interact with Facebook users. In order to post status updates, we first need to create an app.
Create A Facebook App
Go to the Developers area of Facebook and click the “Set Up a New App” button. Follow the steps through setting up your application until you get to the edit settings page. Click the “Facebook Integration” tab and in the Canvas URL and Secure Canvas URL enter a URL to a script on your web server. In this script put the following:
print_r($_GET);
App Permissions
Now we need to set up permissions for the app to post on our behalf. Load the following URL in your browser:
https://www.facebook.com/dialog/oauth?client_id=app_id&redirect_uri=redirect_url&
scope=manage_pages,offline_access,publish_stream
app_id = Application ID (Facebook Integration Tab)
redirect_url = Canvas URL (Facebook Integration Tab)
This will give your app full access to your feed and your pages. The offline_access priv will make it so we don’t have to re-authenticate every time we want to post. See Facebook API Permissions for more info.
After you confirm the app access to your account it should redirect back to your canvas URL script. The canvas URL script should output the GET array which should have a “code” variable. Take the code value and plug it into this URL:
https://graph.facebook.com/oauth/access_token?client_id=app_id&redirect_uri=redirect_url&
client_secret=app_secret&code=code_string
app_id = Application ID
redirect_url = Canvas URL (Facebook Integration Tab)
code_string = code from last URL response
This will output an access token variable. This token will allow us to post to our feed.
Post to Your Feed
Head over to GitHub and pick up the Facebook PHP SDK. Now we finally get into some PHP code. Put the following in a separate script:
// Include facebook class (make sure your path is correct)
require_once("path/to/facebooksdk/src/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'app_id'
'secret' => 'app_secret',
'cookie' => true,
));
//$token is the access token from the URL above
$post = array('access_token' => $token, 'message' => 'new test post - ' . date('Y-m-d'));
try{
$res = $facebook->api('/me/feed','POST',$post);
print_r($res);
} catch (Exception $e){
echo $e->getMessage();
}
This will post a message to your feed. Remember to replace the app_id and app_secret with the info from your app.
Post to Your Friend’s Feed
The access permissions we granted above also give us access to post on our friend’s feed. To do so we just need to change the api call above slightly:
$res = $facebook->api('/friend_username_id/feed','POST',$post);
Replace friend_username_id with the profile id or username of your friend (ie. facebook.com/username or facebook.com/profile.php?id=profile_id). This will also work for fan pages you like.
Post as a Fan Page
You may be an admin of a fan page and want to post as the fan page and not yourself. To do this we just need to take one extra step and get a different access token. Load the following URL:
https://graph.facebook.com/me/accounts?access_token=token
Where token is the access token from above. This will output a new access token for each of the pages you have admin access to. Use this token to post as your fan page instead of yourself.
//$token is the page access token variable from above
$post = array('access_token' => $token, 'message' => 'new test post - ' . date('Y-m-d'));
try{
$res = $facebook->api('/page_username_id/feed','POST',$post);
print_r($res);
} catch (Exception $e){
echo $e->getMessage();
}
Change page_username_id to the ID or username of your page.
The Facebook API was extremely hard for me to grasp as their documentation was very poor. This was the result of a ton of trial and error and I hope it helps you out in your projects.
For more help check out Facebook Developers and Facebook Developer Forums.