Post to WordPress Remotely With XMLRPC in PHP
PLEASE NOTE THAT THIS CODE IS NO LONGER MAINTAINED AND THERE IS NO SUPPORT FOR IT. ASKING QUESTIONS IN THE COMMENTS IS PROBABLY A COMPLETE WASTE OF TIME.
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:
$categories = implode(",", $categories);
$XML = "<title>$title</title>".
$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);
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.