Blog News
| Browse in : |
All
> Topics
> St.Ego's Blog
All > Topics > CMS Blog All > Topics > F.N.F.T. Any of these categories - All of these categories |
Auto-Publishing Articles to Facebook
After my earlier success with getting Xaraya to talk to the Twitter API, I set my sights on completing a matching component for Facebook.
I knew from the start that the code I would need would be simple. Typically, a developer will spend a few hours just to crank out several lines of code, and this was no exception, perfectly in keeping with my expectations.
Publishing to Facebook is a tricky endeavor, but it is now an accomplished feat that you can read about on my Facebook profile even though I never publish to it directly or even visit it myself.
In case you missed my reasoning when I developed the Twitter widget that I'm now using, it is my intention to maintain a single point of entry when I blog. There are just too many different blog sites out there to make use of them all, but many offer an API that can be used to auto-publish your content from a central blog, in this case: Ivory Tower.
The first thing that I had to do was register a new Facebook Application. This is a fairly standard process that you can find plenty of HOWTO's online for, if you dont' end up on the actual App creation page first, in the process.
The important part of creating your new Facebook Application is to set the Callback URL. This is what Facebook will use to ensure you are logged in and have granted the correct level of permission for your application to access your profile.
Since this is all in Xaraya (natch!) I set up a simple Base page for it in my theme (user-main-facebook.xt) and put the FBML (Facebook Markup Language) tag in it for the permission check:
<fb:prompt-permission perms="status_update">Allow Ivory-Tower to post status updates?</fb:prompt-permission>
I set my Facebook Application's Callback URL to:
http://www.ivory-tower.net/index.php?module=base&page=facebook
This completes the connection between your Facebook Application and your Facebook profile. Next comes letting your Articles module publish to Facebook. In my case, I have an Articles publication type set up specifically for Twitter-style entries using just the "title" of the article. At the end of my articles_adminapi_create() function, I hacked an exception in to run additional code in the event that the publication type that I created is being used.
In order to make the connection from Ivory Tower to Facebook, I needed to make use of one of the existing PHP libraries. The first one that I found ended up being the one that I used, but I tried a few others before coming back to it. The chosen API was the Facebook Platform PHP5 Client, published by Facebook.
Unfortunately, that client library was missing a handler for the "setStatus" API call recently released by Facebook. After browsing several forums, I located a snippet to add the setStatus handler to the REST client at the end of the FacebookRestClient class declaration in facebookapi_php5_restlib.php:
public function user_setstatus($status,$clear) {
return $this->call_method('Users.setStatus', array('status' => $status,'clear' => $clear));
}
This (eventually) allowed me to call the new function from within articles_adminapi_create(), right after my Twitter snippet. All it does is take the $title of the new article passed and send it to Facebook as the new text to update your "Status" with, archived on your "Wall". Code follows...
require_once 'modules/facebook/client/facebook.php'; $appapikey = 'YOUR_API_KEY'; $appsecret = 'YOUR_API_SECRET'; $facebook = new Facebook($appapikey, $appsecret); $user_id = $facebook->require_login(); $facebook->api_client->user_setstatus($title,false);
Now, every time I publish a new Twitter-style status update on Ivory Tower, it gets replicated on both Twitter and Facebook now!
One blog to rule them all, one blog to find them...
Note : I should probably take it a step further and set it up using Hooks so that it can be applied to multiple content types, or make it a full module with configuration options to choose a publication type, or provide a way for it publish other types of Facebook content items... but that will have to wait until a client wants it, I'm afraid!
Note: Comments are owned by the poster. We are not responsible for their content.
- Options:
- View Article Map
- View Archives