Working with WordPress XMLRPC- Simple Example

Posted by Joys of Programming on in PHP, Wordpress

How often do you go to WordPress.com to update your blogs? Or you may have a blog using the wordpress blogging platform. You often desired to have some editors which help you to directly blog from your desktops without going to the browser. For this purpose, wordpress also provides the XMLRPC interface. Using the XMLRPC interface, developers can make wordpress clients which help anybody to post from their desktop clients without going to the browser. WordPress XMLRPC supports many other APIs like the blogger API, MetaWebLog apart from its own functions.

Let’s take a simple example which makes of the function wp.getUsersBlogs(). Here we’re going to show a simple PHP program. Note you must substitute your USERNAME and PASSWORD

<?php

$USERNAME = "admin";
$PASSWORD = "check";

function get_response($URL, $context) {
 if(!function_exists('curl_init')) {
 die ("Curl PHP package not installed\n");
 }

 /*Initializing CURL*/
 $curlHandle = curl_init();

 /*The URL to be downloaded is set*/
 curl_setopt($curlHandle, CURLOPT_URL, $URL);
 curl_setopt($curlHandle, CURLOPT_HEADER, false);
 curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
 curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

 /*Now execute the CURL, download the URL specified*/
 $response = curl_exec($curlHandle);
 return $response;
}

  /*Creating the wp.getUsersBlogs request which takes on two parameters
   username and password*/
  $request = xmlrpc_encode_request("wp.getUsersBlogs",
    array($USERNAME, $PASSWORD));

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response("http://localhost/wordpress/xmlrpc.php", $request);
  $response = xmlrpc_decode($xmlresponse);

  if ($response && xmlrpc_is_fault($response)) {
    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  } else {
    /*Printing the response on to the console*/
    print_r($response);
  }
echo "\n";
?>

Now let’s execute this program from the browser or console

$ php wp.getUserBlogs.php

If you get the following ERROR, you must refer the post on enabling WordPress XMLRPC

<?xml version="1.0"?>
<methodResponse>
  <fault>
    <value>
      <struct>
        <member>
          <name>faultCode</name>
          <value><int>405</int></value>
        </member>
        <member>
          <name>faultString</name>
          <value><string>XML-RPC services are disabled on this blog.  An admin user can enable them at http://localhost/wordpress/wp-admin/options-writing.php</string></value>
        </member>
      </struct>
    </value>
  </fault>
</methodResponse>

After enabling wordpress XMLRPC services, you will get the following response

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
        <array><data>
  <value><struct>
  <member><name>isAdmin</name><value><boolean>1</boolean></value></member>
  <member><name>url</name><value><string>http://localhost/wordpress/</string></value></member>
  <member><name>blogid</name><value><string>1</string></value></member>
  <member><name>blogName</name><value><string>Wordpress Test blog</string></value></member>
  <member><name>xmlrpc</name><value><string>http://localhost/wordpress/xmlrpc.php</string></value></member>
</struct></value>
</data></array>
      </value>
    </param>
  </params>
</methodResponse>

Check more on the return value of wp.getUserBlogs. You can learn more about wordpress XMLRPC.

Tags: , ,

Comments:

4 Comments

  • Saulo says:

    Hey, very good post.
    How can i get one by one Return Values?
    For example, how can i show only the blogid or url?

    Thanks since now!

  • Sitebase says:

    Thanks for this example. Just what I needed.
    Do you know if it’s possible to get the current WordPress version from xmlrpc?

  • Joys of Programming says:

    As far as I know, it’s not possible. You can even check the documentation here

  • Maik says:

    Hi,
    thank you very much for your nice description. I will try it with my page and i hope it works well.
    Thank you again
    bets regards
    Maik

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>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.