Monday, 26 April 2010

Facebook status updates to Blogger posts

So after hunting for an online application that would update Blogger with Facebook (found lots for the other way round) I decided to write this little PHP script. It works well, except...
  • While testing I discovered there was a limit to the number of emails that can be sent to the secret email address for Blogger
  • It could do with checking the bounced emails to see which Facebook status updates haven't gone through
You'll need to download simplepie and phpmailer if they aren't already on your host.
You'll also need to set up a cron job to schedule the running of the script. Most hosts have a cpanel with a cron option. Once an hour would be "0 * * * *" and the command would be "/usr/local/bin/php -q /home/yourdirectory/public_html/atomtoemail.php"

I would also highly recommend using a separate file (dbu.php in this case) that contains your email and password - this avoids showing the world your password if the script fails.

You could use a MySQL table for storing the id's but as its only going to be a small file I've kept the flat file.

You can find the Facebook status feed in your browser - look for the RSS icon in the address bar, if there isn't one, then your status updates aren't public.

In theory, this will work with any RSS/ATOM feed.

Heres the code



    Original flatfile DB Author: Michael Shipley (

     Amended by Russell England to email new items (

// Location of simplepie - I've renamed the extension from .inc to .php
// Download from
require_once( '/home/yourdirectory/public_html/includes/simplepie.php');

// db holder
$savedItems = array();

// db file name
$savedItemsFilename = 'saveditems.php';

// max days to keep items in db
$numberOfDays = 14;

$numberOfDaysInSeconds = ($numberOfDays*24*60*60);
$expireDate = time() - $numberOfDaysInSeconds;

// Separate urls with commas for multiple feeds
$urls = array('');
$feed = new SimplePie();

// Set up emailer for Google mail
// Might already be in your includes directory or download from
include "/home/yourdirectory/public_html/includes/phpmailer/class.phpmailer.php"; 

// dbu.php includes the passwords, usernames and emails
include "dbu.php";
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = $mailUsername; // your SMTP username or your gmail username
$mail->Password = $mailPassword; // your SMTP password or your gmail password
$mail->From = $mailFrom;
$mail->FromName = $mailFromName; // Name to indicate where the email came from when the recepient received
$mail->IsHTML(true); // send as HTML

        load flat file db into array
        $savedItems = unserialize(file_get_contents($savedItemsFilename));
                $savedItems = array();

        Loop through items to find new ones and insert them into db
foreach($feed->get_items() as $item)

        // if item is too old dont even look at it
        if($item->get_date('U') < $expireDate)

        // make id
        $id = md5($item->get_id());

        // if item is already in db, skip it

        // Found new item, display on the page for reference and send it off in an email
        echo "<h2>" . $item->get_title() . "</h2>";
        echo "<p>" . $item->get_description() . "</p>";

        $mail->Subject = $item->get_title() ;
        $mail->Body = $item->get_description(); //HTML Body
        $mail->AltBody = $item->get_content(); //Text Body
            echo "Mailer Error: " . $mail->ErrorInfo . "</br>";
            echo "Message has been sent</br>";
        // found new item, add it to db
        $i = array();
        $i['date'] = $item->get_date('U');
        $savedItems[$id] = $i;

        remove expired items from db
$keys = array_keys($savedItems);
foreach($keys as $key)
        if($savedItems[$key]['date'] < $expireDate)

        sort items in reverse chronological order
function customSort($a,$b)
        return $a['date'] <= $b['date'];

        save db
        echo ("<strong>Error: Can't save items.</strong><br>");


No comments:

Post a Comment