May 20, 2009

Add Syndication to symfony-driven Blog

If you have a standalone blog powered by symfony framework, and would like to add syndication with major blog services like Blogger or LiveJournal, you could use a PEAR “Services_Blogging” package.

Download



First, you download it and then install following this tutorial.

Extract the archive and put the contents in your symphony's lib/vendor/serviceblogging directory. You will also have to add the XML-RPC PEAR package. Download it, extract, then put XML_RPC_x.x.x folder in the servicesblogging directory (there must be a Services folder there) and rename from “XML_RPC_x.x.x” to “XML

Install



Now, install the package as a symfony plugin. For this, edit the config/ProjectConfiguration.php:

class ProjectConfiguration extends sfProjectConfiguration
{

static protected $sbLoaded = false;

static public function registerServicesBlogging() {
if (self::$sbLoaded) {
return;
}

set_include_path(sfConfig::get('sf_lib_dir').'/vendor/servicesblogging'
.PATH_SEPARATOR.get_include_path());
require_once sfConfig::get('sf_lib_dir').'/vendor/servicesblogging/Services/Blogging.php';
self::$sbLoaded = true;
}

public function setup()
{
}
}


Enjoy



Now in your php code you can do the following to add a new post:

$bl = Services_Blogging::factory(
"LiveJournal",
"username", "password",
"http://livejournal.com",
"/interface/xmlrpc");

$post = $bl->createNewPost();
$post->title = $title;
$post->content = $text;
$bl->savePost($post);


Disable Auto-formatting



A little something for livejournal users: you can disable auto-formatting if you pass a special parameter to livejournal XML-RPC service. To do this the dirty, way, edit Services/Blogging/Driver/LiveJournal.php file of the Services_Blogging package.

Specifically, in the the Driver.savePost() method, add props parameter to the RPC call value:

...
$value = new XML_RPC_Value(
array(
'username' => $this->userdata['rpc_user'],
'auth_method' => new XML_RPC_Value('challenge', 'string'),
'auth_challenge' => new XML_RPC_Value(
$authdata['challenge'], 'string'
),
'auth_response' => new XML_RPC_Value(
$authdata['response'] , 'string'
),

'subject' => new XML_RPC_Value(
$post->{Services_Blogging_Post::TITLE}
),
'event' => new XML_RPC_Value(
$post->{Services_Blogging_Post::CONTENT}
),
'lineendings' => new XML_RPC_Value('pc'),

'year' => new XML_RPC_Value(date('Y', $time), 'int'),
'mon' => new XML_RPC_Value(date('n', $time), 'int'),
'day' => new XML_RPC_Value(date('j', $time), 'int'),
'hour' => new XML_RPC_Value(date('G', $time), 'int'),
'min' => new XML_RPC_Value(date('i', $time), 'int'),

'props' => new XML_RPC_Value(
array('opt_preformatted' => new XML_RPC_VALUE(true, 'boolean')),
'struct'),
),
'struct'
);

...


That's it.

No comments: