インターネットをかける泡
GoogleやAmazonでもうすっかりおなじみの WebAPI、別名 SOAP、日本語で訳すと泡が最近インターネットで大人気です。PerlでSOAPを使うなら以下のサイトは必見です。
上記の記述を元にHelloモジュールを世界に公開しちゃいましょう。
/home/fuga/test/hello.cgi
SOAP::Transport::HTTP::CGI は自作モジュールを世界に公開します(この場合Helloモジュール)。モジュールの実装を別ファイルで行う場合は、use strict; のあとに use ???; を追加します。
#!/usr/bin/perl -w
use strict;
use SOAP::Transport::HTTP;
SOAP::Transport::HTTP::CGI
-> dispatch_to('Hello')
-> handle;
package Hello;
sub hi {
return "こんにちわ、世界";
}
sub bye {
return "さようなら、世界";
}
sub languages {
return ("パール", "C言語", "シェル");
}
/home/fuga/test/hello_test.cgi
uri のところは便宜上つけた URL とさきほどのモジュール名を書きます。proxy には実際の CGI がある場所を指定します。
#!/usr/bin/perl -w
use strict;
use SOAP::Lite;
my $client = SOAP::Lite
->uri('http://soap.hogehoge.co.jp/Hello')
->proxy('http://dev.hogehoge.co.jp/~fuga/test/hello.cgi');
my $result1 = $client->hi()->result();
my $result2 = $client->bye()->result();
my @rarray1 = $client->languages()->paramsout;
my $result3 = $client->languages()->result;
print "Content-Type: text/plain; charset=euc-jp\n\n";
print "$result1\n$result2\n";
print "結果は「$result3」、その他のパラメータは「@rarray1」\n";
/home/fuga/test/hello_test2.cgi
WSDLファイルを外部に公開する場合はこちらを使います。別途WSDLファイルを用意しなければいけません。
#!/usr/bin/perl -w
use strict;
use SOAP::Lite;
my $client = SOAP::Lite->service("http://soap.hogehoge.co.jp/~fuga/test/Hello.wsdl");
my $result1 = $client->hi();
my $result2 = $client->bye();
my @result3 = $client->languages();
print "Content-Type: text/plain; charset=euc-jp\n\n";
print "$result1\n$result2\n";
print "結果は「@result3」\n";
/home/fuga/test/Hello.wsdl
メソッドを3つ持ったモジュールの定義です。こんなの手書きでなんてやってられません・・。
<wsdl:definitions name="Hello" targetNamespace="http://soap.hogehoge.co.jp/Hello" xmlns:tns="http://soap.hogehoge.co.jp/Hello" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<wsdl:types/>
<!-- definition about input -->
<wsdl:message name="HelloIF_hiRequest">
</wsdl:message>
<!-- definition about output -->
<wsdl:message name="HelloIF_hiResponse">
<wsdl:part name="result" type="xsd:string" />
</wsdl:message>
<!-- definition about module -->
<wsdl:portType name="HelloIF">
<wsdl:operation name="hi" parameterOrder="">
<wsdl:input message="tns:HelloIF_hiRequest" />
<wsdl:output message="tns:HelloIF_hiResponse" />
</wsdl:operation>
<wsdl:operation name="bye" parameterOrder="">
<wsdl:input message="tns:HelloIF_hiRequest" />
<wsdl:output message="tns:HelloIF_hiResponse" />
</wsdl:operation>
<wsdl:operation name="languages" parameterOrder="">
<input message="tns:HelloIF_hiRequest" />
<wsdl:output message="tns:HelloIF_hiResponse" />
</wsdl:operation>
</wsdl:portType>
<!-- definition about method -->
<wsdl:binding name="HelloIFBinding" type="tns:HelloIF">
<wsdl:operation name="hi">
<wsdl:input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="http://soap.hogehoge.co.jp/Hello" />
</wsdl:input>
<wsdl:output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="http://soap.hogehoge.co.jp/Hello" />
</wsdl:output>
<soap:operation soapAction="" />
</wsdl:operation>
<wsdl:operation name="bye">
<wsdl:input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="http://soap.hogehoge.co.jp/Hello" />
</wsdl:input>
<wsdl:output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="http://soap.hogehoge.co.jp/Hello" />
</wsdl:output>
<soap:operation soapAction="" />
</wsdl:operation>
<wsdl:operation name="languages">
<wsdl:input>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="http://soap.hogehoge.co.jp/Hello" />
</wsdl:input>
<wsdl:output>
<soap:body
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
use="encoded" namespace="http://soap.hogehoge.co.jp/Hello" />
</wsdl:output>
<soap:operation soapAction="" />
</wsdl:operation>
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="rpc" />
</wsdl:binding>
<!-- definition about cgi location -->
<wsdl:service name="Hello">
<wsdl:port name="HelloIFPort" binding="tns:HelloIFBinding">
<soap:address
location="http://dev.hogehoge.co.jp/~fuga/test/hello.cgi" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
実行してみる
hi(), bye(), languages() の結果がインターネットを越えてやってきました。結果を eval してやってごにょごにょすれば柔軟なことができるでしょう。
$ ./hello_test.cgi Content-Type: text/plain; charset=euc-jp こんにちわ、世界 さようなら、世界 結果は「パール」、その他のパラメータは「C言語 シェル」