Post to Wordpress Remotely With XMLRPC in PHP

Posted on June 27th, 2008 in Automation, Coding

If you're tired of refreshing this page every day like a d-bag, you should probably subscribe to the RSS feed.

One of Nickycakes’ first projects making cash online started a little less than a year ago and was not much more than an autoblog system that would post articles to wordpress every day.  This thing was the worst piece of memory hogging spaghetti code you’ve ever seen and eventually caused The Great Nickycakes.com Blackout of 2007.  Since it was kinda running on autopilot and the Cakes had so much other crap going on that was making more than the $1 a day or so in adsense revenue that this little script was getting, he just let the thing die and forgot about it.  Having learned quite a bit since then, Nicky has realized that there were many things that were done bass-ackwards that could have been simplified.  One of them was the complete mess of code that was used to make posts to wordpress.  The script actually ran as a wordpress plugin and used WP’s own internal functions to make the posts, which is completely retarded and requires that the script be running on the same machine, which will surely cause you problems if you’re running 100 poorly written memory/cpu hogging scripts on the same shared hosting.

Now armed with a little more knowledge of wordpress and a better general understanding of how to do things properly, the obvious solution to this problem is by posting to wordpress with it’s built in XMLRPC server.  Wordpress lets you submit a simple request, formatted in XML with the login and post details, and your post magically appears on the blog.  This would prove extremely useful if you were to create an army of blogs on different shared hosting accounts and had them all controlled by a central server feeding them the post details.

Anyway, if you were ever inclined to build such a system, or have some other project in mind that needed to send posts to wordpress quickly and easily, here’s a simple 15 line php function for you to do just that:

function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories=array(1)){
$categories = implode(",", $categories);
$XML = "<title>$title</title>".
"<category>$categories</category>".
$body;
$params = array('','',$username,$password,$XML,1);
$request = xmlrpc_encode_request('blogger.newPost',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_exec($ch);
curl_close($ch);
}

This function requires that you have the php xmlrpc and curl modules enabled in php.ini.  $title, $body, $username, and $password are pretty self-explanatory.  $rpcurl is the XMLRPC server address for your wp install.  It’s generally located at http://www.yourblog.com/xmlrpc.php .  $categories is an array of the categories you want the post filed under, and defaults to category 1 which is Uncategorized in the default wordpress installation.  You can most likely use either category numbers or names in this array.

Enjoy.

Published by nickycakes

9 Responses to “Post to Wordpress Remotely With XMLRPC in PHP”

  1. Black Hat Seo Digest Says:

    http://www.tellinya.com/read/2007/10/02/174.html

    <—- Basically it’s the metawebblog api redone/retweaked so it works properly with WP 2+

    Some more advanced options there as well. I was using curl to do my posting, but this library gave me some more options.

    Ed

  2. Sparky Says:

    That’s a nifty idea.

    I prefer going the php-to-mail-to-blog route myself, but this has some definite possibilities. :) Thanks.

  3. underworld Says:

    have to say im afraid 5ubliminal beat u too posting this one - its all about tag adding anyway ;)

  4. Jonathan Volk Says:

    Nice script! :) I think there is huge potential with that

  5. Julian from Pages That Convert Says:

    Thanks for the script! Will definitely come in handy when managing my many mini-sites

  6. Rob Says:

    Yeah, posting via XML-RPC is super useful.

    The only reason we didnt include that feature in this newest release of DP is because you cant future timestamp posts using XMLRPC….so you have to have the script constantly running for every post you want to have uploaded….

    Which is a total pain in the arse!

  7. Wade Says:

    Nice script. Couldn’t you use the email post feature? You setup an email account on your hosting for just adding posts? Then use that email across all the blogs? Unless you are posting different posts per site. Then you just send an email to post@yoursite.com or whatever account you created, and it pops up on all of the blogs? Or is this what caused your blackout?

  8. nickycakes Says:

    What caused the blackout was all the scraper stuff going on at the same time on shitty shared hosting which ended up just overloading it. XML-RPC is better than email because you don’t have to bother setting up email accounts for each blog you want to post to. Simple is better.

  9. Blogging Magazin Says:

    Thanks a lot! Searched a while for just a snippet.

Leave a Comment