Over the past few months I have been trying to connect my blog to my Facebook account so that whenever a post is made on the blog it automatically gets posted on Facebook to varying degree’s of success. Most of the attempts would work for a while and then stop. I even tried using some of the existing plugins for WordPress but since they required a developer account (which needs a valid phone no or CC#) and for some reason I never get the validation code on my cell I was never able to get them to work.
Then I found an article on Linux Magazine on a Command Line interface for Facebook and decided to build on top of that to get the linkage working. Now this is a very hackey way and is not at all elegant or anything but it gets the work done which is what I wanted, so I am good. 🙂 All the work was done in about 2 hours including testing so that should tell you something on its own.
I had to install this on my local system since my webhost didn’t have all the per-requisites to get this to work. That and the fact that I can’t connect to my MySQL db’s from a machine outside of my hosting provider is why this convoluted method was created. The steps I followed to get this to work are as follows.
Install Facebook Commandline
To install Facebook Commandline, follow the instructions on their site.
Authenticate the Application to be able to talk to Facebook
For some reason there was a difference when I run the application from the commandline and when I run it from the web, in as to where the preferences file and the session details were saved, so all the steps have to be done either from the command line or via the web, you can’t interchange the two.
Creating a Web interface for the FBCMD
Since I wanted to be able to get data from WordPress and pass it on to FBCMD I created a new PHP page called run.php that basically pulls the data from WordPress and then passes it to FBCMD as command line parameters. I know that using passthru is probably not very secure and I should have modified the FBCMD file to accept parameters as a URL but didn’t want to spend that much time trying to get this to work. (Hey! I told you it was a quick and dirty ‘fix’).
The contents of this file are very simple:
$handle = fopen('http://www.suramya.com/blog/LatestPost.php', 'r');
$current = fopen('/var/www/fbcmd/latest.dat', 'r');
$current_id = fgets($current, 4096);
$ID = fgets($handle, 4096);
$link = fgets($handle, 4096);
$title = fgets($handle, 4096);
$content = fgets($handle, 596);
$content = chunk_split(htmlspecialchars(strip_tags($content)), 500) . "...";
if($ID != $current_id)
// If we have a new post then call FBCMD to make a post
$command = '/usr/bin/php /var/www/fbcmd/lib/fbcmd/fbcmd.php POST " " "' . chop($title) . '" "' .
chop($link) . '" "' . $content . '"';
// Write the new PostID to a file
$current = fopen('/var/www/fbcmd/latest.dat', 'w');
The file basically calls ‘LatestPost.php’ and gets the latest post details on the blog(see below for details), then it checks if the post made is newer than the last post processed and if so it proceeds to post to Facebook using FBCMD.
‘LatestPost.php’ file looks like this:
require_once( dirname(__FILE__) . '/wp-load.php' );
$month = $_GET['month'];
$year = $_GET['year'];
$args = array( 'numberposts' => 1);
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post);
echo $post->ID . "\n";
This file need to be put on the server in the WordPress Root directory and when called returns an output in the following format:
Once all this is done and the FBCMD has access to post to Facebook all we need is a cron job to run on a frequent basis to run the code. So I created a shell script that contains the following line and have it run every 15 mins.
/usr/bin/curl http://localhost/fbcmd/run.php > /tmp/FBPost.out
That’s it. So far it looks like its working great and if this post shows up on my FB wall then all is well. If not, then its back to the code to see what went wrong this time.