Unexpected error processing XML-RPC request.
EOD exit 0; } # Send an XML document (but don't exit). sub send_xml ($) { my ($xml_string) = @_; my $length = length($xml_string); print <<"EOD"; Status: 200 OK Content-type: text/xml Content-length: $length EOD # We want precise control over whitespace here. print $xml_string; } You can copy the utility routines into your own CGI scripts. ----------------------------------------------------------------------------- 7. Using XML-RPC with Python Fredrik Lundh has provided an excellent XML-RPC library for Python. To install, download the latest version. You can either stick the *.py files in the same directory as your Python code, or you can install them in your system's Python directory. RedHat 6.2 users can type the following: bash$ mkdir xmlrpclib-0.9.8 bash$ cd xmlrpclib-0.9.8 bash$ unzip ../xmlrpc-0.9.8-990621.zip bash$ python python> import xmlrpclib python> import xmlrpcserver python> Control-D bash$ su -c 'cp *.py *.pyc /usr/lib/python1.5/' We import two of the *.py files to trick Python into compiling them. Users of other platforms should consult their Python documentation. For more Python examples, see the article XML-RPC: It Works Both Ways on the O'Reilly Network. ----------------------------------------------------------------------------- 7.1. A Python Client The following program shows how to call an XML-RPC server from Python: import xmlrpclib # Create an object to represent our server. server_url = 'http://xmlrpc-c.sourceforge.net/api/sample.php'; server = xmlrpclib.Server(server_url); # Call the server and get our result. result = server.sample.sumAndDifference(5, 3) print "Sum:", result['sum'] print "Difference:", result['difference'] ----------------------------------------------------------------------------- 8. Using XML-RPC with C and C++ To get a copy of XML-RPC for C/C++, see the [http://xmlrpc-c.sourceforge.net] xmlrpc-c website. You can either download everything in RPM format, or you can build it from source. ----------------------------------------------------------------------------- 8.1. A C Client Save the following code in a file called getSumAndDifference.c: #includeCould not connect to HTTP server.
"; } elseif ($result->faultCode()) { print "XML-RPC Fault #" . $result->faultCode() . ": " . $result->faultString(); } else { $struct = $result->value(); $sumval = $struct->structmem('sum'); $sum = $sumval->scalarval(); $differenceval = $struct->structmem('difference'); $difference = $differenceval->scalarval(); print "
Sum: " . htmlentities($sum) . ", Difference: " . htmlentities($difference) . "
"; } ?> If your webserver doesn't run PHP scripts, see the [http://www.php.net/] PHP website for more information. ----------------------------------------------------------------------------- 10.2. A PHP Server The following script shows how to implement an XML-RPC server using PHP. getParam(0); $x = $xval->scalarval(); $yval = $params->getParam(1); $y = $yval->scalarval(); // Build our response. $struct = array('sum' => new xmlrpcval($x + $y, 'int'), 'difference' => new xmlrpcval($x - $y, 'int')); return new xmlrpcresp(new xmlrpcval($struct, 'struct')); } // Declare our signature and provide some documentation. // (The PHP server supports remote introspection. Nifty!) $sumAndDifference_sig = array(array('struct', 'int', 'int')); $sumAndDifference_doc = 'Add and subtract two numbers'; new xmlrpc_server(array('sample.sumAndDifference' => array('function' => 'sumAndDifference', 'signature' => $sumAndDifference_sig, 'docstring' => $sumAndDifference_doc))); ?> You would normally invoke this as something like http://localhost/path/ sumAndDifference.php. ----------------------------------------------------------------------------- 11. Using XML-RPC with Microsoft .NET Charles Cook is writing an excellent new chapter for this HOWTO. You can find a draft online. If that page disappears, look for a new version of the XML-RPC HOWTO at the Linux Documentation Project. ----------------------------------------------------------------------------- 12. Using XML-RPC with Ruby (This section of the XML-RPC HOWTO was generously provided by Michael Neumann.) [http://www.ruby-lang.org/] Ruby is an object-oriented scripting language. It already has a major following in Japan, and it's becoming popular elsewhere. To use XML-RPC with Ruby, you must first install Yoshida Masato's xmlparser module (a wrapper for James Clark's expat parser). This can be found at the Ruby Application Archive. Then, you must install [http://www.s-direktnet.de/homepages/neumann/xmlrpc4r/ index.html] xmlrpc4r using the following commands: bash$ tar -xvzf xmlrpc4r-1_2.tar.gz bash$ cd xmlrpc4r-1_2 bash$ su root -c "ruby install.rb" ----------------------------------------------------------------------------- 12.1. A Ruby Client 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}" ----------------------------------------------------------------------------- 12.2. A Ruby Server 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) ----------------------------------------------------------------------------- 13. Using XML-RPC with Proprietary Languages Implementations of XML-RPC are available for the following proprietary programming languages and environments. ----------------------------------------------------------------------------- 13.1. Using XML-RPC with K (This section of the XML-RPC HOWTO was generously provided by Christian Langreiter.) [http://www.kx.com/] K is a language used in finance and database development. To install XML-RPC for K, download it from the Kx website. Uncompress and copy the files into the directory in which you keep your .k programs. 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))}) More examples are included in the distribution. ----------------------------------------------------------------------------- 14. Applications with Built-in XML-RPC Support Several popular Linux applications include support for XML-RPC. These have already been described elsewhere, so we mostly provide pointers to articles. ----------------------------------------------------------------------------- 14.1. Zope Articles on using XML-RPC with Zope are available elsewhere on the web: * XML-RPC Programming with Zope by Jon Udell * Zope XML-RPC at UserLand.Com ----------------------------------------------------------------------------- 14.2. KDE 2.0 KDE 2.0 includes Kurt Ganroth's kxmlrpc daemon, which allows you to script KDE applications using XML-RPC. Here's a short sample application in Python. It shows you how to connect to kxmlrpc, manipulate your KDE address book, and query the KDE trader. If you have any other articles or example code, please see Section 15.3. We'd like to have more information on scripting KDE. ----------------------------------------------------------------------------- 15. About This Document This document is part of the Linux Documentation Project. Thanks go to Dave Winer and maintainers of all the various XML-RPC libraries. ----------------------------------------------------------------------------- 15.1. New Versions of This Document New versions of this document are available at the Linux Documentation Project website. They also provide this manual in alternate formats, including [http:// www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/XML-RPC-HOWTO.pdf] PDF, gzipped HTML and ASCII text. ----------------------------------------------------------------------------- 15.2. Contributors to the XML-RPC HOWTO The section on Ruby was contributed by Michael Neumann (neumann AT s-direktnet DOT de). The section on K was contributed by Christian Langreiter (c DOT langreiter AT synerge DOT at). These contributors are all credited at the beginning of their sections. ----------------------------------------------------------------------------- 15.3. Submitting Other Snippets If you have a sample client or server in another language or environment, we'd love to include it in this manual. To add a new entry, we need the following information: * The URL of the XML-RPC implementation used. * Installation instructions. * A complete, runnable program. * Compilation instructions, if applicable. E-mail your example to the xmlrpc-c-devel mailing list or directly to [mailto:eric.kidd@pobox.com] Eric Kidd. Thank you for your help!