XML-RPC HOWTO Eric Kidd Source Builders And Other Contributors 野本浩一 - 日本語翻訳 Copyright ? 2001 Eric Kidd 0.8.0, 2001-04-12 Revision History Revision 0.8.0 2001-04-12 Updated section on common interfaces. Added pointer to XML-RPC.Net information. Revision 0.7.0 2001-04-03 Added section on C++ proxy classes. Revision 0.6.0 2001-02-02 Added sections on Ruby, K and common interfaces. Revision 0.5.0 2001-01-23 Initial version. 様々な言語でクライアントおよびサーバを実装するための XML-RPC の使用方法 を述べます。例題のプログラムは Perl, Python, C, C++, Java, PHP および他 の言語で示しています。Zope や KDE 2.0 についても扱っています。XML-RPC のサポートはすべてのオペレーティングシステムで利用できます。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Table of Contents 1. Legal Notice 2. XML-RPC とは? 2.1. どのように動作するのか 2.2. サポートされるデータの種別 2.3. XML-RPC の歴史 3. XML-RPC 対他のプロトコル 3.1. XML-RPC 対 CORBA 3.2. XML-RPC 対 DCOM 3.3. XML-RPC 対 SOAP 4. 一般的な XML-RPC インターフェース 4.1. イントロスペクション - サーバの API を掘り出す 4.2. Boxcarring - 同時に複数の要求を送る 5. API の事例 - sumAndDifference 6. Perl での XML-RPC の使い方 6.1. Perl クライアント 6.2. 独立型 Perl サーバ 6.3. CGI ベースの Perl サーバ 7. Python での XML-RPC の使い方 7.1. Python クライアント 8. C および C++ での XML-RPC の使い方 8.1. C クライアント 8.2. C++ クライアント 8.3. 代替クラス (Proxy Class) での C++ クライアント 8.4. CGI ベースの C サーバ 9. Java での XML-RPC の使い方 9.1. Java クライアント 9.2. 独立型 Java サーバ 10. PHP での XML-RPC の使い方 10.1. PHP クライアント 10.2. PHP サーバ 11. Microsoft .NET での XML-RPC の使い方 12. Ruby での XML-RPC の使い方 12.1. Ruby クライアント 12.2. Ruby サーバ 13. 独自的な言語での XML-RPC の使い方 13.1. K での XML-RPC の使い方 14. XML-RPC サポートが組み込まれたアプリケーション 14.1. Zope 14.2. KDE 2.0 15. この文書について 15.1. この文書の新版 15.2. XML-RPC HOWTO の寄稿者 15.3. 他の情報の提出 16. 日本語訳について 1. Legal Notice Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License , Version 1.1 or any later version published by the Free Software Foundation with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. You may obtain a copy of the GNU Free Documentation License from the Free Software Foundation by visiting their Web site or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. This manual contains short example programs ("the Software"). Permission is hereby granted, free of charge, to any person obtaining a copy of the Software, to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following condition: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2. XML-RPC とは? XML-RPC は HTTP 上でリモート手続き呼び出しを行 うための簡単で手軽な方法です。Perl, Java, Python, C, C++, PHP や多くの 他のプログラミング言語で使用できます。Unix と Windows, Macintosh 上で利 用可能です。 Perl で書かれた短い XML-RPC クライアントを示します (Ken MacLeod の Frontier::Client を使用します) 。 use Frontier::Client; $server = Frontier::Client->new(url => 'http://betty.userland.com/RPC2'); $name = $server->call('examples.getStateName', 41); print "$name\n"; 実行すると、このプログラムはリモートのサーバに接続し、州名を得て、それ を表示します (この例の州番号 41 は South Dakota のはずです)。 Python での同じプログラムです (これでは Fredrik Lundh の xmlrpclib を使います)。 python> import xmlrpclib python> server = xmlrpclib.Server("http://betty.userland.com/RPC2") python> server.examples.getStateName(41) 'South Dakota' 以降の章では、様々な言語で XML-RPC クライアントおよびサーバの書き方を学 びます。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1. どのように動作するのか XML-RPC は Dave Winer の公式仕様書 で詳細に 説明されています。興味があるなら、是非ご覧になってください。簡潔で明解 な文書です。 通信する上で、XML-RPC の値は XML にエンコードされます - sample.sumAndDifference 5 3 上記は冗長ですが、簡単に圧縮できます。また、予想以上に速いです - Rick Blair の測定によれば、Hannes Walln fer の Java での実装を使うと XML-RPC 呼び出しの応答時間は 3 ミリ秒です。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.2. サポートされるデータの種別 XML-RPC は以下のデータの種別をサポートします。 int 符号付 32 ビット整数 string ASCII 文字列で空バイトも許されます。(実際にはたくさんの XML-RPC の 実装が Unicode をサポートしています。XML の基本機能に感謝。) boolean 真か偽のどちらか double 倍精度浮動小数点実数 (実際にはいくつかの実装で使えないかもしれませ ん) dateTime.iso8601 日付と時刻。残念ながら、XML-RPC はタイムゾーンの使用を禁止している ので、これはほとんど役に立ちません。 base64 任意長の生のバイナリデータ - 通信上 Base64 を使用してエンコードされ ます。とても役立ちます (けれどもいくつかの実装では 0 バイトのデータ 受信が行えません)。 array 値の一次元配列。個々の値はどんな種別でもかまいません struct キーと値の集まり。キーは文字列です - 値はどんな種別でもかまいません 。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.3. XML-RPC の歴史 XML-RPC は、先行する二つのプロトコルから着想を得ています。その一つは、 anonymous RPC であり、これは Dave Winer により設計され、過去に DaveNet でのエッセイ として発表されたものです (XML-RPC サーバが /RPC2 下にインストールされる ことが多いのはこのためです)。もう一つの、より重要な着想は、SOAP プロト コルの初期ドラフトによるものです。 より詳しい XML-RPC の歴史 という文書を Dave Winer が提供してくれています。この文 書では、XML-RPC と SOAP との関係についても説明されています。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3. XML-RPC 対他のプロトコル リモート手続き呼び出しは XML-RPC が唯一の方法ではありません。人気のある 他のプロトコルには CORBA, DCOM, SOAP などがあります。各プロトコルには長 所と短所があります。 この章での意見には、(著者の) 明らかな偏見も含まれています。どうか、その 点を考慮してお読みください。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.1. XML-RPC 対 CORBA CORBA は人気のあるプロトコルで、分散型オブジェ クト指向アプリケーション向けに書かれています。一般的に複数の層を持つエ ンタープライズアプリケーションで使用されます。最近では、アプリケーショ ン間通信用にも Gnome プロジェクトによって選定さ れています。 CORBA は多くのベンダーやたくさんのフリーソフトウェアプロジェクトにより 、よくサポートされています。CORBA は Java, C++ とうまく動作し、多くの他 の言語からも利用可能です。さらに CORBA は優れた interface definition language (IDL) を提供し、読みやすいオブジェクト指向 API を定義できるよ うにします。 残念なことに、CORBA はとても複雑です。学習曲線は急勾配となっていて、実 装するのに非常な努力を必要とし、かなり高度なクライアントを必要とします 。分散型ウェブアプリケーションよりもエンタープライズやデスクトップアプ リケーションに一番適しています。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.2. XML-RPC 対 DCOM DCOM は CORBA に対する Microsoft の答えです。すでに COM コンポーネントを使用していて Microsoft 以外のシステムとやり取りする必要がなければ、素晴らしいものです。そうで なければ、あまり役に立たないでしょう。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3. XML-RPC 対 SOAP SOAP はとても XML-RPC に似ています。XML 文 書のまま、HTTP 上で整列した手続き呼び出しを行う動作も似ています。残念な がら SOAP は仕様の確定に悩んでいるように見えます。 SOAP は元々 UserLand, DevelopMentor, Microsoft の共同制作により作成され ました。最初の公のリリースは基本的にネームスペースと長いエレメント名を 持つ XML-RPC でした。しかしその後 SOAP は W3C ワーキンググループに引き 継がれました。 残念なことに、ワーキンググループは SOAPにとても多くの不可解な機能の追加 を行っています。この文書の執筆時点で、SOAP は XML スキーム、エミュレー ション、奇妙な複合構造体および配列、そしてカスタムタイプをサポートして います。加えて、いくつもの SOAP の外部仕様の実装も定義されています。 基本的に XML-RPC が好きであるけれどもっとたくさんの機能を持ったプロトコ ルを望む場合、SOAP を調査するといいでしょう。 :-) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4. 一般的な XML-RPC インターフェース いくつかの XML-RPC サーバはビルトインメソッドを提供します。それらは XML-RPC それ自身のものではありませんが、とても便利な追加機能です。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.1. イントロスペクション - サーバの API を掘り出す Edd Dumbill は次のメソッドの組みを提案 しました。 array system.listMethods () string system.methodHelp (string methodName) array system.methodSignature (string methodName) サーバがこれらのメソッドをサポートする場合、あるドキュメンテーションを 印字するためにサーバへ問い合わせができます - import xmlrpclib server = xmlrpclib.Server("http://xmlrpc-c.sourceforge.net/api/sample.php") for method in server.system.listMethods(): print method print server.system.methodHelp(method) print これらのメソッドは PHP および C、Microsoft .NET で書かれたサーバにより サポートされています。部分的なイントロスペクションのサポートは最近にな って UserLand Frontier にも組み込まれています。Perl および Python, Java 用のイントロスペクションのサポートは XML-RPC Hacks のページで入手できます。どうぞ、遠 慮なくイントロスペクションのサポートを他の XML-RPC サーバに追加してくだ さい! 様々なクライアント側のツール (ドキュメンテーション、ラッパー生成等等) も XML-RPC Hacks のページで見つけられます。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2. Boxcarring - 同時に複数の要求を送る 多くのファンクションコールをする XML-RPC クライアントを書いているなら、 インターネットのバックボーンのレーテンシー (待ち時間) が短いおかげで、 応答時間がかなり速いことを気づくかもしれません。いくつかのサーバは次の 機能を使うことで複数の要求を一括処理 (batching) できます。 array system.multicall (array calls) 追加情報は system.multicall RFC より得てください。 現在、このメソッドは C および UserLand Frontier で書かれたサーバでサポ ートされています。Python および Perl で書かれたサーバは XML-RPC Hacks のページにあるプログラムを 利用できます。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5. API の事例 - sumAndDifference XML-RPC のデモをするために、可能な限り多くの言語で以下に示します。 struct sample.sumAndDifference (int x, int y) この関数は引き数として二つの整数を受け取って、二つのエレメントを持つ XML-RPC を戻します - sum 二つの整数の和 difference 二つの整数の差 実用的なものではありませんが、立派な例題になっています :-) この関数 (や他のもの) は URL http://xmlrpc-c.sourceforge.net/api/ sample.php を利用しています (この URL はブラウザでは何もできず、XML-RPC クライアントで利用できます)。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6. Perl での XML-RPC の使い方 Ken MacLeod は Perl 用 XML-RPC を実装しています。彼の Frontier::RPC モ ジュールは彼のウェブサイト もし くは CPAN を通じて入手できます。 Frontier::RPC をインストールするには、標準的なやり方でパッケージをダウ ンロードしてコンパイルします - bash$ gunzip -c Frontier-RPC-0.07b1.tar.gz | tar xvf - bash$ cd Frontier-RPC-0.07b1 bash$ perl Makefile.PL bash$ make bash$ make test bash$ su -c 'make install' (ウィンドウ環境やルート権限の無い場合、わずかに違った手順となるでしょう 。詳細は Perl の文書を参考にしてください。) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.1. Perl クライアント 次のプログラムは Perl から XML-RPC サーバのコールの方法を示します。 use Frontier::Client; # Make an object to represent the XML-RPC server. $server_url = 'http://xmlrpc-c.sourceforge.net/api/sample.php'; $server = Frontier::Client->new(url => $server_url); # Call the remote server and get our result. $result = $server->call('sample.sumAndDifference', 5, 3); $sum = $result->{'sum'}; $difference = $result->{'difference'}; print "Sum: $sum, Difference: $difference\n"; ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.2. 独立型 Perl サーバ 次のプログラムは Perl での XML-RPC サーバの書き方を示します。 use Frontier::Daemon; sub sumAndDifference { my ($x, $y) = @_; return {'sum' => $x + $y, 'difference' => $x - $y}; } # Call me as http://localhost:8080/RPC2 $methods = {'sample.sumAndDifference' => \&sumAndDifference}; Frontier::Daemon->new(LocalPort => 8080, methods => $methods) or die "Couldn't start HTTP server: $!"; ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3. CGI ベースの Perl サーバ Frontier::RPC2 は CGI ベースサーバ用のサポートは本来備えられていません 。しかし大丈夫、必要なほとんどの機能は用意されています。 次のプログラムコードを sumAndDifference.cgi として、ウェブサーバの cgi-bin ディレクトリに保存してください (Unix システム上で、chmod +x sumAndDifference.cgi と入力し、実行属性を与える必要があるでしょう)。 #!/usr/bin/perl -w use strict; use Frontier::RPC2; sub sumAndDifference { my ($x, $y) = @_; return {'sum' => $x + $y, 'difference' => $x - $y}; } process_cgi_call({'sample.sumAndDifference' => \&sumAndDifference}); #========================================================================== # CGI Support #========================================================================== # Simple CGI support for Frontier::RPC2. You can copy this into your CGI # scripts verbatim, or you can package it into a library. # (Based on xmlrpc_cgi.c by Eric Kidd .) # Process a CGI call. sub process_cgi_call ($) { my ($methods) = @_; # Get our CGI request information. my $method = $ENV{'REQUEST_METHOD'}; my $type = $ENV{'CONTENT_TYPE'}; my $length = $ENV{'CONTENT_LENGTH'}; # Perform some sanity checks. http_error(405, "Method Not Allowed") unless $method eq "POST"; http_error(400, "Bad Request") unless $type eq "text/xml"; http_error(411, "Length Required") unless $length > 0; # Fetch our body. my $body; my $count = read STDIN, $body, $length; http_error(400, "Bad Request") unless $count == $length; # Serve our request. my $coder = Frontier::RPC2->new; send_xml($coder->serve($body, $methods)); } # Send an HTTP error and exit. sub http_error ($$) { my ($code, $message) = @_; print <<"EOD"; Status: $code $message Content-type: text/html $code $message

$code $message

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 EOD # We want precise control over whitespace here. print $xml_string; } あなた自身の CGI スクリプトにユーティリティルーチンをコピーしてもかまい ません。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7. Python での XML-RPC の使い方 Fredrik Lundh は優れたPython 用 XML-RPC ライブラリ を提供しています。 インストールするために、最新版をダウンロードしてください。 *.py ファイ ルを個人の Python プログラムとして、個人のディレクトリに突っ込んでも、 システムの Python ディレクトリにインストールしてもかまいません。 RedHat 6.2 のユーザは次のように入力します。 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/' 上記では、Python に二つの *.py をコンパイルさせるために、それらを import しています。他のプラットフォームでは Python の文書を参考にしてく ださい。 さら多くの Python での例に関しては、O'Reilly network の XML-RPC: It Works Both Ways の記事をみてください。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.1. Python クライアント 次のプログラムは Python から XML-RPC サーバのコールの方法を示します。 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. C および C++ での XML-RPC の使い方 C/C++ 用 XML-RPC のコピーを得るには、xmlrpc-c のウェブサイト を見てください。 RPM 形式ですべてのものをダウンロードすることも、ソースから自分で環境を 構築することもできます。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.1. C クライアント 次のプログラムコードを getSumAndDifference.c としてファイルに保存してく ださい。 #include #include #include #define NAME "XML-RPC getSumAndDifference C Client" #define VERSION "0.1" #define SERVER_URL "http://xmlrpc-c.sourceforge.net/api/sample.php" void die_if_fault_occurred (xmlrpc_env *env) { /* Check our error-handling environment for an XML-RPC fault. */ 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; xmlrpc_int32 sum, difference; /* Start up our XML-RPC client library. */ xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION); xmlrpc_env_init(&env); /* Call our XML-RPC server. */ result = xmlrpc_client_call(&env, SERVER_URL, "sample.sumAndDifference", "(ii)", (xmlrpc_int32) 5, (xmlrpc_int32) 3); die_if_fault_occurred(&env); /* Parse our result value. */ xmlrpc_parse_value(&env, result, "{s:i,s:i,*}", "sum", &sum, "difference", &difference); die_if_fault_occurred(&env); /* Print out our sum and difference. */ printf("Sum: %d, Difference: %d\n", (int) sum, (int) difference); /* Dispose of our result value. */ xmlrpc_DECREF(result); /* Shutdown our XML-RPC client library. */ xmlrpc_env_clean(&env); xmlrpc_client_cleanup(); return 0; } コンパイルするための入力を示します - bash$ CLIENT_CFLAGS=`xmlrpc-c-config libwww-client --cflags` bash$ CLIENT_LIBS=`xmlrpc-c-config libwww-client --libs` bash$ gcc $CLIENT_CFLAGS -o getSumAndDifference getSumAndDifference.c $CLIENT_LIBS あなたのシステムの C コンパイラの名前が gcc ではないかもしれないので、 実際の入力ではそれを代える必要があるかもしれません。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.2. C++ クライアント 次のプログラムコードを getSumAndDifference2.cc としてファイルに保存して ください。 #include #include #define NAME "XML-RPC getSumAndDifference C++ Client" #define VERSION "0.1" #define SERVER_URL "http://xmlrpc-c.sourceforge.net/api/sample.php" static void get_sum_and_difference () { // Build our parameter array. XmlRpcValue param_array = XmlRpcValue::makeArray(); param_array.arrayAppendItem(XmlRpcValue::makeInt(5)); param_array.arrayAppendItem(XmlRpcValue::makeInt(3)); // Create an object to resprent the server, and make our call. XmlRpcClient server (SERVER_URL); XmlRpcValue result = server.call("sample.sumAndDifference", param_array); // Extract the sum and difference from our struct. XmlRpcValue::int32 sum = result.structGetValue("sum").getInt(); XmlRpcValue::int32 diff = result.structGetValue("difference").getInt(); cout << "Sum: " << sum << ", Difference: " << diff << endl; } int main (int argc, char **argv) { // Start up our client library. XmlRpcClient::Initialize(NAME, VERSION); // Call our client routine, and watch out for faults. try { get_sum_and_difference(); } catch (XmlRpcFault& fault) { cerr << argv[0] << ": XML-RPC fault #" << fault.getFaultCode() << ": " << fault.getFaultString() << endl; XmlRpcClient::Terminate(); exit(1); } // Shut down our client library. XmlRpcClient::Terminate(); return 0; } コンパイルするための入力を示します - bash$ CLIENT_CFLAGS=`xmlrpc-c-config c++ libwww-client --cflags` bash$ CLIENT_LIBS=`xmlrpc-c-config c++ libwww-client --libs` bash$ c++ $CLIENT_CFLAGS -o getSumAndDifference2 getSumAndDifference2.cc $CLIENT_LIBS この作業のために、かなり最近の C++ コンパイラが必要となります。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.3. 代替クラス (Proxy Class) での C++ クライアント XML-RPC サーバがイントロスペクション API をサポートしているなら、自動的 にそのサーバ用の C++ 代替クラスを生成できます。代替クラスを生成するため には、次のコマンドを入力してその出力をファイルに保存してください。 bash$ xml-rpc-api2cpp \ > http://xmlrpc-c.sourceforge.net/api/sample.php sample SampleProxy これは sample で始まるすべてのメソッド用のラッパーが含まれている SampleProxy と名づけられた代替クラスを生成します。あなたが望む方法でこ のクラスを修正してもかまいません。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.4. CGI ベースの C サーバ 次のプログラムコードを sumAndDifference.c としてファイルに保存してくだ さい。 #include #include xmlrpc_value * sumAndDifference (xmlrpc_env *env, xmlrpc_value *param_array, void *user_data) { xmlrpc_int32 x, y; /* Parse our argument array. */ xmlrpc_parse_value(env, param_array, "(ii)", &x, &y); if (env->fault_occurred) return NULL; /* Return our result. */ return xmlrpc_build_value(env, "{s:i,s:i}", "sum", x + y, "difference", x - y); } int main (int argc, char **argv) { /* Set up our CGI library. */ xmlrpc_cgi_init(XMLRPC_CGI_NO_FLAGS); /* Install our only method (with a method signature and a help string). */ xmlrpc_cgi_add_method_w_doc("sample.sumAndDifference", &sumAndDifference, NULL, "S:ii", "Add and subtract two integers."); /* Call the appropriate method. */ xmlrpc_cgi_process_call(); /* Clean up our CGI library. */ xmlrpc_cgi_cleanup(); } コンパイルするための入力を示します - bash$ CGI_CFLAGS=`xmlrpc-c-config cgi-server --cflags` bash$ CGI_LIBS=`xmlrpc-c-config cgi-server --libs` bash$ gcc $CGI_CFLAGS -o sumAndDifference.cgi sumAndDifference.c $CGI_LIBS これが終了したら、sumAndDifference.cgi をウェブサーバの cgi-bin ディレ クトリにコピーしてください。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9. Java での XML-RPC の使い方 Hannes Walln fer は Java 用 XML-RPC の優れた実装 を提供しています。 インストールは、配布物をダウンロードして、unzip して、 CLASSPATH に *.jar ファイルを追加します。Unix システム上で、次の入力によりこの作業が 行えます。 bash$ unzip xmlrpc-java.zip bash$ cd xmlrpc-java/lib bash$ CLASSPATH=`pwd`/openxml-1.2.jar:`pwd`/xmlrpc.jar:$CLASSPATH ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.1. Java クライアント 次のプログラムコードを JavaClient.java としてファイルに保存してください 。 import java.util.Vector; import java.util.Hashtable; import helma.xmlrpc.*; public class JavaClient { // The location of our server. private final static String server_url = "http://xmlrpc-c.sourceforge.net/api/sample.php"; public static void main (String [] args) { try { // Create an object to represent our server. XmlRpcClient server = new XmlRpcClient(server_url); // Build our parameter list. Vector params = new Vector(); params.addElement(new Integer(5)); params.addElement(new Integer(3)); // Call the server, and get our result. Hashtable result = (Hashtable) server.execute("sample.sumAndDifference", params); int sum = ((Integer) result.get("sum")).intValue(); int difference = ((Integer) result.get("difference")).intValue(); // Print out our result. System.out.println("Sum: " + Integer.toString(sum) + ", Difference: " + Integer.toString(difference)); } catch (XmlRpcException exception) { System.err.println("JavaClient: XML-RPC Fault #" + Integer.toString(exception.code) + ": " + exception.toString()); } catch (Exception exception) { System.err.println("JavaClient: " + exception.toString()); } } } ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.2. 独立型 Java サーバ 次のプログラムコードを JavaServer.java としてファイルに保存してください 。 import java.util.Hashtable; import helma.xmlrpc.*; public class JavaServer { public JavaServer () { // Our handler is a regular Java object. It can have a // constructor and member variables in the ordinary fashion. // Public methods will be exposed to XML-RPC clients. } public Hashtable sumAndDifference (int x, int y) { Hashtable result = new Hashtable(); result.put("sum", new Integer(x + y)); result.put("difference", new Integer(x - y)); return result; } public static void main (String [] args) { try { // Invoke me as . WebServer server = new WebServer(8080); server.addHandler("sample", new JavaServer()); } catch (Exception exception) { System.err.println("JavaServer: " + exception.toString()); } } } ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10. PHP での XML-RPC の使い方 Edd Dumbill は PHP 用 XML-RPC を実装しています。UsefulInc XML-RPC ウェ ブサイト からダウンロードできます。 配布物をインストールするには、それを解凍して、 xmlrpc.inc と xmlrpcs.inc をあなたの PHP スクリプトが入っているのと同じディレクトリに コピーします。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.1. PHP クライアント 次のスクリプトはウェブページの中に XML-RPC コールを組み込む方法を示しま す。 XML-RPC PHP Demo

XML-RPC PHP Demo

send($message); // Process the response. if (!$result) { print "

Could 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) . "

"; } ?> あなたのウェブサーバが PHP スクリプトを実行しない場合、追加情報を得るた めに PHP のウェブサイト を見てください。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.2. PHP サーバ 次のスクリプトは PHP を利用した XML-RPC サーバの実装の方法を示します。 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))); ?> あなたは通常、 http://localhost/path/sumAndDifference.php のようなもの を呼び出すでしょう。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11. Microsoft .NET での XML-RPC の使い方 Charles Cook はこの HOWTO 用の優れた新しい章を執筆中です。草稿はオンラ イン で見られます。そのペ ージが姿を消したら、Linux Documentation Project で XML-RPC HOWTO の新版 を探してくださ い。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12. Ruby での XML-RPC の使い方 (XML-RPC HOWTO のこの章は Michael Neumann が気前よく提供してくれました 。) Ruby はオブジェクト指向スクリプト言語です。 既に日本では多くの愛好者がおり、他所でも人気が出つつあります。 Ruby で XML-RPC を使うには、初めに吉田正人の xmlparser モジュール (James Clark の expat parser 用ラッパー) をインストールしなければなりま せん。これは Ruby Application Archive で見つけられます。 そして次のコマンドを使って xmlrpc4r をインストールしなければなりませ ん。 bash$ tar -xvzf xmlrpc4r-1_2.tar.gz bash$ cd xmlrpc4r-1_2 bash$ su root -c "ruby install.rb" ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.1. Ruby クライアント 単純な Ruby クライアントを示します - 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. Ruby サーバ 単純な Ruby サーバを示します - require "xmlrpc/server" s = XMLRPC::CGIServer.new s.add_hanlder("sample.sumAndDifference") do |a,b| { "sum" => a + b, "difference" => a - b } end s.serve また、次のように書くこともできます。 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 どちらのサーバも独立型で実行するには、プログラムの二行目を s = XMLRPC::Server.new(8080) に書きかえてください。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13. 独自的な言語での XML-RPC の使い方 XML-RPC の実装は以下に示す独自的なプログラミング言語および環境で利用で きます。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.1. K での XML-RPC の使い方 (XML-RPC HOWTO のこの章は Christian Langreiter が気前よく提供してくれま した。) K は会計処理およびデータベース開発で使われる言語で す。K 用 XML-RPC をインストールするには Kx のウェブサイト からダウンロードします。解凍してあなたの .k プ ログラムを保持しているディレクトリにファイルをコピーします。 短いクライアントを示します - \l kxr server:("localhost";"/cgi-bin/sumAndDifference.cgi"); .kxr.c[server;"sumAndDifference";2 3] そしてサーバを示します - \l kxrsc .kxr.sm: ,("sumAndDifference";{.((`sum;+/x);(`difference;-/x))}) さらに多くの例が配布物の中にあります。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14. XML-RPC サポートが組み込まれたアプリケーション いくつかの一般的な Linux アプリケーションは XML-RPC のサポートをしてい ます。これらは既に他のところで説明がされているので、ここでは主にその記 事へのポインタを示します。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.1. Zope Zope での XML-RPC の使用に関する記事は次に示すウェブから入手できます。 ・ Jon Udell による Zope での XML-RPC プログラミング ・ UserLand.Com の Zope XML-RPC ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.2. KDE 2.0 KDE 2.0 は Kurt Ganroth の kxmlrpc デーモン を含み、デーモンは XML-RPC を使った KDE アプリケーションのためのお膳立てをします。 Python で書かれた短いアプリケーションの例 があります。それは kxmlrpc へ 接続する、 KDE の住所録を操作する、KDE トレーダを検索するといった方法を 示します。 あなたがその他に記事やプログラムの例を持っている場合、 Section 15.3> を 見てください。私達は KDE を記述する上でもっと多くの情報を持ちたいと思っ ています。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15. この文書について この文書は Linux Documentation Project に所 蔵されています。Dave Winer および様々な XML-RPC ライブラリのすべての管 理者に感謝します。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.1. この文書の新版 この文書の新版は Linux Documentation Project のウェブサイトで入手できます。 また次のフォーマットでも提供されています。 PDF , gzipped HTML および ASCII text 。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.2. XML-RPC HOWTO の寄稿者 Ruby に関する章は Michael Neumann (neumann AT s-direktnet DOT de) によ り寄稿されました。K に関する章は Christian Langreiter (c DOT langreiter AT synerge DOT at) により寄稿されました。 [訳注:上記の名前の後の () 内はメールアドレスを示しています。「AT」は「 @」、「DOT」は「.」に置きかえて、空白を除去すればメールアドレスが得られ ます。] 寄稿者はその章の始めに記入されています。 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 15.3. 他の情報の提出 別の言語や環境でのクライアントやサーバの例を持っている場合、この文書に 追加したいととても思ってます。新しいエントリを追加するために、次に示す 情報が必要です。 ・ XML-RPC を使用した実装の URL ・ インストール手順 ・ 完全な実行できるプログラム ・ 必要ならコンパイル手順 文書に記載してもかまわない E-mail アドレス - 例えば xmlrpc-c-devel メー リングリスト もしくは、直接 の Eric Kidd 。 情報をお待ちしています! ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16. 日本語訳について 日本語訳は Linux Japanese FAQ Project が行いました。翻訳に関するご意見 は JF プロジェクト 宛に連絡してください。 0.8.0j 翻訳: 野本浩一 校正: □ 千旦裕司さん □ 山田慎也さん