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:

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.

Peanut Gallery

  • 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

  • That’s a nifty idea.

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

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

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

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

  • 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!

  • 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?

  • 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.

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

  • Thanks for posting the snippet!

    Question: How would you use this code to include WP tags?

  • I think the categories act as tags.

  • Nice script. I was victim of that same resource hogging utilizing the platform for the actual post thing myself. I don’t have time for MFA sites though. Waste of time to me.

  • Hi! i’m trying your script but i’m getting this error:

    Fatal error: Call to undefined function xmlrpc_encode_request() in /Applications/MAMP/htdocs/wordpress/wordpress/myPost.php on line 16

    I was looking for extensions or external libraries on php for using that function and it seems it’s not necessary, can you help me with that? :)

    Thanks in advance!

  • you are the man :) i searched 2 days and finally find this page.

    i have 1 question. you wrote

    $request = xmlrpc_encode_request(‘blogger.newPost’,$params);

    ‘blogger.newPost’ what is this code? why blogger?

  • Apologies for asking the obvious as I’m not much of a coder and having trouble getting it run!

    Could you just give an example of the code needed to run the function?

    ie if my variables were..

    $title = “Test Title”;
    $body = “This is the post body”;
    $rpcurl = “http://www.mysite.com/blogtest/xmlrpc.php”;
    $username = “admin”;
    $password = “password”;
    $categories = array(1,3);

    How would I run the function?

    As I said – apologies if this is obvious to everyone, but I’ve spent ages with my limited knowledge trying to get it to work!

    Many thanks in advance!

    Gary

  • sounds like you guys need a basic php book

  • So is that a ‘no’ then? lol!

    I’m sure you started out sometime and would have welcome just a little help without having to trawl through for hours trying to work stuff out!

    I’m sure it would have taken you as long to post the implementation code as it did your comment!

    So – many thanks in advance. I’m sure the Universe will thank you in return some other way :-)

  • I’m looking at this real quick, but it sounds like NickyCakes is right, you deffinetly need a PHP book! This is just a function you can call from wherever basically. Look up out to call a function in PHP! There are 4 variables that need to be passed, you set them right, but now you need to pass that to the function! function(parameters,paramaters2);

    Somethin like that, thanks for the script kidd, I’ll be using this for sure..

  • is there any charset restrictions? i want to use special characters in iso-8859-9 charset. bu it always saying “faultCode -32700 faultString parse error. not well formed”.

    +

    and i want that book too :))

  • I’ve gone full circle looking for a PHP function that will allow me to use wp.newPage to post a a new static page.

    I’ve used the function here to successfully create a new post. I tried to modify the function to use wp.newPage but could never get it to work.

    Is there any PHP code or scripts to get wp.newPage working?

  • Nice script.
    I notice it includes wp-admin … so this means the script needs to sit in the same folder as the wp install, that right?

    So if I wanted to post remotely to 100 different blogs then I’d need a copy of the script in every install folder, that right?

    Is there a way to have one script that sits for example in a utilities folder and runs thru a db or whatever and posts to multiple blogs?

    Cheers.

  • Opps … forget the bit in my last post about wp-admin include .. did I really write that??

    I’d just woken up, thats my excuse anyway … the rest of the stuf is still valid tho … i think!!

    also, if the script is running but not actually posting anything, is there anything that can be added to help debugging?

  • Ok, so after doing a fair impression of ‘fuckwit of the year’ in my previous two posts … I have something proper to say:

    Problem was, I was running the script and nothing was happening … no posts were being added. So after preventing my two remaining brain cells from fighting with each other and pointing them in the same direction, here’s what I found .. maybe it’s old news, maybe not but, for what it’s worth:

    “WordPress 2.6 onwards will be more secure out-of-the box including changes to disable the remote publishing protocols by default.”

    I’m running 2.7 so mine were ‘disabled’.

    So I went to the Settings->Writing page and enable XML-RPC, ran the script again and … it worked perfectly …
    Cheers!

  • thanks for the awesome script, it is the frst actually usable code i could find – after searchig for hours. Any way you can add a line to make it possible to create new categores? I have been trying but so far cant get it to work – tried doing something like –
    $params = array(”,$username,$password,’my_category’,”,”,”,”);
    //$request = xmlrpc_encode_request(‘wp.newCategory’, ’1′, $username, $password, $newcat);
    $request = xmlrpc_encode_request(“wp.newCategory”, $params);

  • Haha I just saw the first comment and I’am currently also using it for something “bad” ;-). It’s great but I also need an script for adding tags to the posts :-(.

  • Hello, very nice code snippet. Thanks…

  • thanks for code snippet . it was helpful lot.. how can i post to blogger remotely with xmlrpc

  • Thanks for this wonderful piece of code, I’ve been searching for a while for this…

    It works fine but I can’t get the categories right… On my code, $categories is an array of category names that don’t exist and I expect the xmlrpc.php file to create those categories… Is that possible ?

    I also tried to use category numbers (I tried $categories=array(1,2) ) and it created 2 new categories in my blog, named “1″ and “2″… The same is not working with names :(

    And I’d love to be able to specify the tags of the post, can you tell me where can I have an example of all the xml tags I can use in the $XML variable ? (I’ve searched all over wordpress.org and other places but had no luck… I’ve tried to use and it does not work either)

    Thanks in advance :)

    by João Jerónimo
  • Is it working with WP 2.8.X?

  • The no tags on any “auto post” is definitely a “footprint” in the eyes of google, you’ve been warned!!

  • why cant someone just post a php file of a working script with examples of all variables etc. not all of us here are php pros..

    i cant get this script to work at all :/

  • I have enabled the xml-rpc support via Settings > Writing

    When I visit /mysite.com/xmlrpc.php I get the correct ‘Only accepts POST’ message…

    However your script doesn’t seem to work…

    Here’s the full php code I’m using… can anyone shed any light?

    $title = ‘Test Title’;
    $body = ‘Body Text’;
    $rpcurl = ‘http://mysite.com/wordpress/xmlrpc.php’;
    $username = ‘admin’;
    $password = ‘mypassword’;
    $categories[0] = “none”;
    $categories[1] = “none1″;

    function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories=array(1)){
    $categories = implode(“,”, $categories);
    $XML = “$title” . “$categories”.
    $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);
    }

  • So I went to the Settings->Writing page and enable XML-RPC, ran the script again and … it worked perfectly …

  • also are the open sourcing the entire platform or just a few components??

  • I’m getting:

    Call to undefined function xmlrpc_encode_request()

    I’ve enables XML RPC in wordpress, but i don’t know how to activate it in php.ini

    I’m using 1and1 as my host. I’d appreciate any help. Thanks so much!

  • Wow, a year and a half after writing this article it is still coming up in oogle!

    Anyway, very informative and has been very useful info for me.
    Thanks

  • Nice script!
    very informative and has been very useful info for me.

  • I have been looking for alternative to post-by-email. this is the one! thanks

  • Hey, thanks for sharing this info. This is just what I was looking for.

  • This script looks like it would have worked really well, but I think WordPress 2.9.2 is asking for XML-RPC POST requests only, here's the error I'm getting

    XML-RPC server accepts POST requests only.

  • Great i been looking for this information shame other sites can’t print out without the all waste.

  • Thank you Nickcakes for these clear explanations. Great work.

  • I have created a script which will help you to add, edit, get recent posts of WordPress from outside WordPress Admin panel.

    http://www.hurricanesoftwares.com/wordpress-xmlrpc-posting-content-from-outside-wordpress/

    Check out the code examples. You may need to download IXR Library to handle XML-RPCl

  • Hey

    I am using this script and it works perfectly. However, it sometimes returns false even tho the post is added to the blog.

    Do you know why this happens?

    - Stian

  • Wow, it’s working.
    Previously, I wanted to post my posts into the database. But this method is much more easier. Thanks

  • so discussing the code in this forum is discouraged!.
    Its ok, to not support your previous code, but why discourage discussion. ??

Reply

Add a new comment