TheDrunkenEpic - Drunken Ramblings of a Code Monkey
Filed under

php

 

A Testament to my Laziness

One of the biggest problems I've always had with maintaining a blog is the maintaining part. I often find myself too busy, too indifferent, too burned out or, more often than not, too brain dead to care. With the advent of Twitter, things have gotten even worse. I no longer have to sit and think about how to make a worth-while blog post based on a single, tiny string of thought.

Thanks to Twitter, blogging, for the most part, is pretty much dead. Want to share something cool? Well, you no longer have to write a few paragraphs about it, just post the damn thing to Twitter or even Facebook. Thanks to software like TweetDeck, and other push notification services, I can not only post to multiple twitter accounts, I can even update my Facebook status; all with a single click.

Publishing minutea is now easier than ever and you don't have to spend hours making 5 paragraphs from a simple 140 character message.

So, it is because of this small problem I threw together another Posterous integration script. This is basically a cron script I run once a week from the main site which pulls all of my tweets from the previous 7 days, compiles them into a digest and publishes them to my default Posterous blog.

As you can see from my previous entry it's already working. I figure this is a great stop-gap measure which keeps content flowing to the site in between real blog posts.

There are times when I just don't have the time or energy to spend on a decent blog post. As you can see from my archives, good and worthwhile entries are few and far between. Hopefully, this won't always be the case, but at the moment small measures such as this will have to do.

If anyone is interested, I threw this script up on my Github account as a public repository. Feel free to fork it and create something better. It isn't the prettiest block of code I've written, but I prefer the KISS method for something like this.

Here's to Twitter and micro-blogging!

Loading mentions Retweet
Filed under  //   Code Monkey   Development   Free Goodies   php  

Comments [0]

Import Content Into Posterous Using PHP + cURL + RSS ...

Before I moved to Posterous from Symphony, I had to take a serious thought as to how I was going to transfer my old content into my new account. I didn't want to manually transfer my articles over and Posterous doesn't offer any kind of import feature for my system. Besides, either way, I was going to lose all of my visitor's comments. I actually considered trashing all my old content and start fresh just so I wouldn't have to deal with the issue.

So, obviously, I decided to go ahead and find an easy way to move my stuff to a new home. By using a combination of PHP, cURL and RSS I was able to easily accomplish this within minutes. Since I'm such a nice guy, I'll go ahead and show you how I did it.

After digging around this service, I found that Posterous provides a very simple REST-based API which, among other things, allows you to post new content to one of your sites.

I then realized I could use my current site's RSS feed as the source of my content. I went ahead and changed the settings to this feed to display every article I ever wrote on the site.

Next, I cracked my knuckles and got to work writing the following script:

<?php 

set_time_limit(0);

define('IMPORT_RSS', 'http://www.mysite.com/'); // A direct link to the RSS feed you wish to import posts from ...
define('IMPORT_SITE_ID', 69); // The id of one of your Posterous sites ...
define('IMPORT_SITE_EMAIL', 'my@email.com'); // The email address assigned to your Posterous account ...
define('IMPORT_SITE_PASSWORD', 'password'); // The password assigned to your Posterous account ...

$RSS = new SimpleXMLElement(file_get_contents(IMPORT_RSS));

foreach($RSS->channel->item as $Entry)
{
$values = array
(
'site_id' => IMPORT_SITE_ID,
'title' => $Entry->title,
'body' => $Entry->description,
'date' => $Entry->pubDate,
);

$curl = curl_init('posterous.com/api/newpost');

curl_setopt($curl, CURLOPT_USERPWD, IMPORT_SITE_EMAIL . ':' . IMPORT_SITE_PASSWORD);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $values);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_exec($curl);

curl_close($curl);
}

Before you run this script, be sure you have PHP5 compiled with cURL support. Simply change the above constants to their appropriate values, make sure your RSS feed is valid, upload the script and execute it in your browser. If your values are correct, you will start noticing your articles appearing in the proper order in your Posterous account.

Sweet!

Clearly, there are fancier ways to do this, but there's no need to create overly elaborate code I'm only going to be running once.

NOTE:

One thing you should realize when using the API, the REST end points should NOT include any part of the 'http://www.' prefix. Doing so will cause authentication errors even if you're using the correct credentials. This stalled me for a good hour or so and with the limited documentation, I was left to resolve the issue with no help. Hopefully, if you're having the same problem, this will help you out. If not, contact the Posterous support peeps.

Till next time!

Loading mentions Retweet
Filed under  //   api   curl   php   posterous   rss   xml  

Comments [1]