Indonesian

XML-RPC Drivers

Driver libraries for many platforms are available and can be easily found on the internet. For example, the default library used by Carex2X is the Incutio IXR XML-RPC library for PHP.

XML-RPC Libraries

Libraries available for the following platforms.

  • PHP
  • C
  • C++
  • Python
  • Perl (Frontier)
  • Java
  • Microsoft .NET
  • Ruby
  • K



    
    // Specifying a client by host, path and port
    
    $client = new IXR_Client('scripts.incutio.com', '/xmlrpc/simpleserver.php', 80);
    
    if (!$client->query('test.add', 3, 4)) {
      die('Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage());
    }
    
    echo $client->getResponse();
    





/* A simple synchronous XML-RPC client written in C. */

#include <stdio.h>

#include <xmlrpc.h>
#include <xmlrpc_client.h>

#define NAME "XML-RPC C Test Client"
#define VERSION "0.1"

void die_if_fault_occurred (xmlrpc_env *env)
{
   if (env->fault_occurred) {
       fprintf(stderr, "XML-RPC Fault: %s (%d)\n",
               env->fault_string, env->fault_code);
       exit(1);
   }
}

int main (int argc, char** argv)
{
   xmlrpc_env env;
   xmlrpc_value *result;
   char *state_name;
   
   /* Start up our XML-RPC client library. */
   xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION);

   /* Initialize our error-handling environment. */
   xmlrpc_env_init(&env);

   /* Call the famous server at UserLand. */
   result = xmlrpc_client_call(&env, "http://betty.userland.com/RPC2",
   "examples.getStateName",
   "(i)", (xmlrpc_int32) 41);
   die_if_fault_occurred(&env);
   
   /* Get our state name and print it out. */
   xmlrpc_parse_value(&env, result, "s", &state_name);
   die_if_fault_occurred(&env);
   printf("%s\n", state_name);
   
   /* Dispose of our result value. */
   xmlrpc_DECREF(result);

   /* Clean up our error-handling environment. */
   xmlrpc_env_clean(&env);

   /* Shutdown our XML-RPC client library. */
   xmlrpc_client_cleanup();

   return 0;
}





// List recently-released Linux applications. (Written in C++.)
// For more details about O'Reilly's excellent Meerkat news service, see:
// http://www.oreillynet.com/pub/a/rss/2000/11/14/meerkat_xmlrpc.html */

#include <iostream.h>
#include <strstream.h>

#include <XmlRpcCpp.h>

#define NAME           "XML-RPC C++ Meerkat Query Demo"
#define VERSION        "0.1"
#define MEERKAT_URL    "http://www.oreillynet.com/meerkat/xml-rpc/server.php"
#define SOFTWARE_LINUX (6)

static void list_apps (int hours) {

   // Build our time_period parameter.
   ostrstream time_period_stream;
   time_period_stream << hours << "HOUR";
   string time_period = time_period_stream.str();

   // Assemble our meerkat query recipe.
   XmlRpcValue recipe = XmlRpcValue::makeStruct();
   recipe.structSetValue("category", XmlRpcValue::makeInt(SOFTWARE_LINUX));
   recipe.structSetValue("time_period", XmlRpcValue::makeString(time_period));
   recipe.structSetValue("descriptions", XmlRpcValue::makeInt(76));

   // Build our parameter array.
   XmlRpcValue param_array = XmlRpcValue::makeArray();
   param_array.arrayAppendItem(recipe);

   // Create a client pointing to Meerkat.
   XmlRpcClient meerkat (MEERKAT_URL);

   // Perform the query.
   XmlRpcValue apps = meerkat.call("meerkat.getItems", param_array);

   // Print our results.
   int first = 1;
   size_t app_count = apps.arraySize();
   for (int i = 0; i < app_count; i++) {
XmlRpcValue app = apps.arrayGetItem(i);

// Get some information about our application.
string title       = app.structGetValue("title").getString();
string link        = app.structGetValue("link").getString();
string description = app.structGetValue("description").getString();

// Print a separator line if necessary.
if (first)
    first = 0;
else
    cout << endl;

// Print this application entry.
if (description.size() > 0) {
    cout << title << endl << description << endl << link << endl;
} else {
    cout << title << endl << description << endl << link << endl;
}
   }
}

// Print out a usage message.
static void usage (void)
{
   cerr << "Usage: meekat-app-list [hours]" << endl;
   cerr << "Data from <http://www.oreillynet.com/meerkat/>." << endl;
   exit(1);
}

int main (int argc, char **argv) {
   int status = 0;
   int hours = 25;

   // Parse our command-line arguments.
   if (argc == 1) {
// Use default value for hours.
   } else if (argc == 2) {
hours = atoi(argv[1]);
   }
   if (hours == 0)
usage();
   if (hours > 49) {
       cerr << "It's not nice to ask for > 49 hours at once." << endl;
       exit(1); 
   }

   // Start up our client library.
   XmlRpcClient::Initialize(NAME, VERSION);

   // Call our implementation, and watch out for faults.
   try {
list_apps(hours);
   } catch (XmlRpcFault& fault) {
cerr << argv[0] << ": XML-RPC fault #" << fault.getFaultCode()
     << ": " << fault.getFaultString() << endl;
status = 1;
   }

   // Shut down our client library.
   XmlRpcClient::Terminate();

   return status;
}



  • Python

    1. Source

      The library is integrated in Python starting at version 2.2

    2. Sample Python code of an xmlrpc query

from xmlrpclib import Server

betty = Server("http://betty.userland.com")

print betty.examples.getStateName(41)



  • Perl

    1. Source

      The library can be downloaded from http://bitsko.slc.ut.us/~ken/xml-rpc/

    2. Sample Perl code of an xmlrpc query

use Frontier::Client;

$server = Frontier::Client->new(url => $url);

$result = $server->call($method, @args);







  // Java example
  XmlRpcClient xmlrpc=new XmlRpcClient
    ("http://xmlrpc.emailservice.com:80/xmlrpcInterface");
  Vector params=new Vector();
  params.addElement("myUserName");
  Integer result=(Integer)xmlrpc.execute
    ("email.getNumberNewMessages", params);



  • Microsoft .NET

    1. Source

      The library and more information can be found at

      http://www.xml-rpc.net/faq/xmlrpcnetfaq.html

    2. Sample Microsoft .NET code of a client side proxy class.



[XmlRpcUrl("http://www.cookcomputing.com/sumAndDiff.rem")] 
class SumAndDiffProxy : XmlRpcClientProtocol 
{ 
  [XmlRpcMethod("samples.sumAndDifference")] 
  SumAndDiffValue SumAndDifference(int x, int y) 
  { 
    return (SumAndDiffValue)Invoke("SumAndDifference", 
                                   new Object[]{ x, y }); 
  } 
} 



  • Ruby

    1. Source

      The library can be downloaded from here: http://xmlrpc-c.sourceforge.net/xmlrpc-howto/xmlrpc-howto-ruby.html

    2. Sample Ruby code of an xmlrpc query

      Here's a simple Ruby client:

          require "xmlrpc/client"
          
          # Make an object to represent the XML-RPC server.
          server = XMLRPC::Client.new( "xmlrpc-c.sourceforge.net", "/api/sample.php")
          
          # Call the remote server and get our result
          result = server.call("sample.sumAndDifference", 5, 3)
          
          sum = result["sum"]
          difference = result["difference"]
          
          puts "Sum: #{sum}, Difference: #{difference}"

      Here's a simple Ruby server:

          require "xmlrpc/server"
          
          s = XMLRPC::CGIServer.new
          
          s.add_hanlder("sample.sumAndDifference") do |a,b|
            { "sum" => a + b, "difference" => a - b }
          end
          
          s.serve

      This could also have been written as follows:

          require "xmlrpc/server"
          
          s = XMLRPC::CGIServer.new
          
          class MyHandler
            def sumAndDifference(a, b)
              { "sum" => a + b, "difference" => a - b }
            end
          end
          
          s.add_handler("sample", MyHandler.new)
          s.serve

      To run either server in standalone mode, replace the second line of code with the following:

          s = XMLRPC::Server.new(8080)



  • K

    1. Source

      The library can be downloaded from here http://www.langreiter.com/k/kxr

    2. Sample K code of an xmlrpc query

      Here's a short client:

          \l kxr
          server:("localhost";"/cgi-bin/sumAndDifference.cgi");
          .kxr.c[server;"sumAndDifference";2 3]

      And here's a server:

          \l kxrsc
          .kxr.sm: ,("sumAndDifference";{.((`sum;+/x);(`difference;-/x))})


©2004 HXP All rights reserved