Automatic posting to Blogger blogs with PHP

Yes, you can autopost to blogspot (blogger) blogs with php, and it’s a lot easier than you might think.

Similar to the way that plugins like wp-o-matic work for automatically posting rss feeds to Wordpress blogs, you can have a blogger blog which is automatically populated with RSS feed content, at intervals that you determine (by a cron job).

Blogger blogs allow the blogs owner to post via email, so we’re going to use PHP’s mail function to post new content, and PHP’s SimpleXML to gather the content before posting.

Firstly, we need to set up our blogger blog to accept emailed posts: We need to go to our settings page in Blogger, and click the “Settings” tab at the top. Then click the “email” button.

You’re now at the Email settings page, look for the Mail-to-Blogger Address. Here you just need to enter a password (in the box before the “@ablogger.com”) that you’ll use in your emails to post to the blogger blog.

Once you’ve chosen your password, write down the email address, as this is the email address we’ll be using in the PHP script to tell it where to post the rss feed to.

Now on to the script:


//Your Blog%u2019s Keyword:
$keyword = %u201Ckeyword%u201D;

//How many articles do you want to grab each time?
$num = 5;

//Get the RSS Feed - In this instance, we%u2019re using a google blogsearch feed based on our chosen keyword
$feed = simplexml_load_file(%u201Dhttp://blogsearch.google.com/blogsearch_feeds?hl=en&scoring=d&q=%u201D .urlencode($keyword). %u201C&ie=utf-8&num=10&output=rss%u201D);

//Loop through our keywords
foreach ($feed->channel->item as $item) {

if($i < $num){

//Have a bit of a rest so we%u2019re not posting too fast to our blogger blog
sleep(10);

$title = $item->title;
$title = str_replace(%u201D%u201D, %u201C%u201D, $title);
$subject = str_replace(%u201D
%u201D, %u201C%u201D, $title);
$link = $item->link;

$description = $item->description;
$description = str_replace(%u201D%u201D, %u201C%u201D, $description);
$body = str_replace(%u201D
%u201D, %u201C%u201D, $description);

//put our secret blogger email address here:
$to = %u201Caccountname.password@blogger.com%u201D;

//ignore this line - the script just needs something in the %u201CFrom%u201D field.
$headers = %u2018From: mail@whatever.com%u2019;

//Send the email / How%u2019d we go?
if(mail($to, $subject, $body, $headers)) {
echo $subject. %u201D - sent
%u201D;
}
else
{
echo $subject. %u201D - NOT sent
%u201D;

}
}
//add one to our counter
$i ;

}

?>


And that’s it.

Customise the script to suit your purposes, upload to your server, and call it via a cron job.

site visitor