WordPress XML-RPC Intro

I’m still getting a few emails regularly asking for help. Taking into account the fact that blog not being updated for a while, this is surprising. One of the latest questions was “Is it worth to buy XYZ autoblogging software?”. I didn’t answer; I asked him “How are you going to get your money back? What’s your plan?” but he couldn’t explain. I sent him WordPress XML-RPC examples so he could code a small app and think about ROI plan.

Tools can’t help you to keep your business afloat when you don’t understand the business. A fool with a tool is still a fool. Some people just don’t get it. If you’re one of them, stop being a pussy. It’s time to make crucial step toward business success ;} Tell your gf that you need a day off from her, take a glass of beer, open favorite IDE, and learn something. Asking questions is good, but answering is more important. Start from autoblogging or whatever is Ok for you.


And here are a few WP XML-RPC examples I mentioned above. But before you start coding go through this reading list:

Then get a copy of class-IXR.php from WP sources (which is actually IXR The Incutio XML-RPC Librarydownload) and make sure remote publishing settings are enabled (Settings > Writing Settings > Remote Publishing) on test blog.

Categories

A good way to start with WP XML-RPC API is categories listing.

<?php
include 'class-IXR.php';
 
$log = 'login';
$pwd = 'password';
$xmlrpc = 'http://www.yourblog.com/xmlrpc.php';
 
$client = new IXR_Client($xmlrpc);
 
// Debug ON. Now you know what's going on.
$client->debug = true;
 
$res = $client->query('wp.getCategories', '', $log, $pwd);
?>

query method returns either true (Ok) or false (smth wrong). Hence we need a better way to display results. getResponse, isError, getErrorCode and getErrorMessage methods is what you need.

<?php
echo "\nResponse "; print_r($client->getResponse());
echo "\nIs Error ", $client->isError();
echo "\nError Code ", $client->getErrorCode();
echo "\nError Message ", $client->getErrorMessage();
?>

Categories are obviously necessary when you setup a new blog. You can keep predefined categories in txt/db and shoot them all at once.

<?php
$res = $client->query('wp.newCategory', '', $log, $pwd,
    array(
        'name' => 'MyCategory',
        'slug' => 'my-category-slug',
        'parent_id' => 0,
        'description' => 'Test category by xml-rpc'
    )
);
?>

Delete unnecessary category.

<?php
$res = $client->query('wp.deleteCategory', '', $log, $pwd, $category_id);
?>

Posts

New post. Params are obvious.

<?php
$data = array(
    'title' => 'Post Title',
    'description' => 'Weight loss ...',
    'dateCreated' => (new IXR_Date(time())),
    'mt_allow_comments' => 1,
    'mt_allow_pings' => 1,
    'categories' => array('Weight Loss','Supplements')
);
$published = 1; // 0 - draft, 1 - published
$res = $client->query('metaWeblog.newPost', '', $log, $pwd, $data, $published);
?>

Scheduling is easy if you know what you’re doing.

<?php
$time = time(); // get current time
 
foreach ($posts as $post) {
    // ...
    $data = array(
        'title' => 'Post Title',
        'description' => 'Weight loss ...',
        'dateCreated' => (new IXR_Date($time)),
        'mt_allow_comments' => 1,
        'mt_allow_pings' => 1,
        'categories' => array('Weight Loss','Supplements')
    );
    // post ...
    $time += 86400; // next day 24 * 60 * 60 
}
?>

Pages

Getting pages.

<?php
// all
$res = $client->query('wp.getPages', 0, $log, $pwd);
 
// only one
$res = $client->query('wp.getPage', 0, $page_id, $log, $pwd);
?>

And creating them.

<?php
$res = $client->query('wp.newPage', 0, $log, $pwd,
    array(
        'wp_slug' => 'test-page-slug',
        'wp_password' => '',
        'wp_page_parent_id' => 0,
        'wp_page_order' => 0,
        'wp_author_id' => 0,
        'title' => 'Test Page Title',
        'description' => "Page content",
        'mt_excerpt' => '',
        'mt_text_more' => '',
        'mt_allow_comments' => 1,
        'mt_allow_pings' => 0,
        'dateCreated' => (new IXR_Date(time())),
        array(), // custom fields
        0 // 0 - draft, 1 - published
    )
);
?>

Bonus – How to ping blogs using cURL?

<?php
function http_get_post($method, $proxy, $url, $referrer, $agent, $header,
    $timeout, $post_fields) {
 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_REFERER, $referrer);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
 
    if (!is_null($proxy)) {
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
        curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
    }
 
    if (strtolower($method) == 'get') {
        curl_setopt($ch, CURLOPT_HTTPGET, 1);
        curl_setopt($ch, CURLOPT_POST, 0);
    } elseif (strtolower($method) == 'post') {
        if (!empty($post_fields)) {
            curl_setopt($ch, CURLOPT_HTTPGET, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
        } else {
            die('Postfields missing');
        }
    } else {
        die("Unexpected HTTP method $method");
    }
 
    $result['EXE'] = curl_exec($ch);
    $result['INF'] = curl_getinfo($ch);
    $result['ERR'] = curl_error($ch);
 
    curl_close($ch);
 
    return $result;
}
 
$src = 'http://www.yourblog.com/post/';
$target = 'http://www.targetblog.com/post/';
$target_xmlrpc = 'http://www.targetblog.com/xmlrpc.php';
 
$pingxml = '<?xml version="1.0"?>'
    . '<methodCall><methodName>pingback.ping</methodName><params>'
    . "<param><value><string>$src</string></value></param>"
    . "<param><value><string>$target</string></value></param>"
    . '</params></methodCall>';
 
$result = http_get_post('post', null, $target_xmlrpc, '',
    'The Incutio XML-RPC PHP Library', 1, 5, $pingxml);
?>

Going Further

Posting is nothing special. Think about combining it with the best spinner PHP API. And you can post a product feed too. Mash with flickr, youtube vids and move to next blog.

14 Responses to WordPress XML-RPC Intro

  1. This post is why fromzerotoseo.com is one of my fav reads.

    thanks!

  2. I wanted this script since long. Thanks for this. I will surely use this.

  3. This is a little bit off-topic but still about XML-RPC, I was wondering.. if you have that as default… why is it that Google and the other search engines will rank that page? I’ve seen this way too many times.

    -Jean

    • Sorry, I don’t get it. Which page?

      • Hi, referring to xmlrpc.php, just check out Google… for example if you type site:www.usedtiresblog.com/xmlrpc.php and even with your website, if you put fromzerotoseo website it also does the same thing it shows up as a page result. Is Google suppose to list that result? I’ve always wondered how/why Google does list it in the first place.

        Till then,

        Jean

  4. The script is good to use. Thanks

  5. Just wanted to take the time and wish you a Merry Christmas and best of wishes for 2011, Hopefully we get a new post soon, ;-) would love to learn and read more :D

    -Jean

  6. Awesome Xml coding to do WP XML-RPC API. Its really looking now more easy to
    implement. I would say thanks for the blog owner, who posts such nice article..

  7. Your above mentioned code helpful for a newbie n also for expert.XML is wonderful language and using nowadays in many platforms.The script above you shared good for all…

  8. I have a co worker that does just that. Always looking for a tool instead of just doing his job. I don’t understand what the obsession with all these different tools has become. The old way works just as well and with some of the tweaks and everything else that’s gone on it still works.

  9. Given Examples having lots of good information about WP XML-RPC and i like this line regarding business : “Tools can’t help you to keep your business afloat when you don’t understand the business”

  10. I am with the above comment you can have all the tools you need but if you don’t understand the business you can’t do much.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>