📄 ch14_02.htm
字号:
<html><head><title>SOAP::Lite (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch14_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">14.2. SOAP::Lite</h2><p>You can try an implementation of SOAP with Perl called <a name="INDEX-1865" />SOAP::Lite,written by <a name="INDEX-1866" />Paul Kulchenko. SOAP::Lite is a collectionof Perl modules that provide a simple and lightweight interface forboth the client and server side. This version of SOAP::Lite supportsthe <a name="INDEX-1867" />SOAP 1.1 specification(<a href="http://www.w3.org/TR/SOAP">http://www.w3.org/TR/SOAP</a>) and works with many<a name="INDEX-1868" />SOAPimplementions, including Apache SOAP, Frontier, Microsoft SOAP,Microsoft .NET, DevelopMentor, XMethods, 4s4c, Phalanx, Kafka,SQLData, Lucin (in Java), Perl, C++, Python, VB, COM, and XSLT.</p><p>Here's an example that uses SOAP::Lite. Thefollowing code creates a SOAP::Lite object and uses it, but what'sspecial about the SOAP::Lite object is that it'sbased on code that lives on www.soaplite.com. In other words, the codethat lives on the remote end of the connection might not be Perl atall—but by using SOAP::Lite, you can execute the <tt class="literal">f2c()</tt> function as if you were calling <tt class="literal">f2c()</tt> from your own Perl program.</p><p>Also note the <span class="option">uri</span><a name="INDEX-1869" /> and<span class="option">proxy</span><a name="INDEX-1870" />options. The <span class="option">uri</span> option is the URI for SOAP methods.Basically, <span class="option">uri</span> tells SOAP::Lite where SOAP-relatedcontent lives. In Perl terms, <span class="option">uri</span> can represent theclass in which you'll find the function to execute.For example, let's say you have the following SOAPserver that uses HTTP as its transport mechanism:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse SOAP::Transport::HTTP;SOAP::Transport::HTTP::CGI -> dispatch_to('Hello::(?:hello)') -> handle ;</pre></blockquote><p>This example has two key parts: it will look for a module named"Hello" and will execute<tt class="literal">Hello::hello( )</tt> when called. Note that youdon't have to explicitly"require"<em class="filename">Hello.pm</em>; SOAP::Lite does this for you. Now,let's look at the client:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse SOAP::Lite;# If you want to see why something isn't working, enable +trace after# use SOAP::Lite, as shown below #use SOAP::Lite +trace; my $proxy = 'http://www.some.server/cgi-bin/hello'; print SOAP::Lite -> uri('urn:Hello') -> proxy($proxy) -> hello() -> result . "\n\n" ;</pre></blockquote><p>It's essential that <span class="option">uri</span>, as usedabove, matches the module in which the <tt class="literal">hello( )</tt>function lives. If it doesn't, youwon't see any returned results, since SOAP::Litewon't be able to find the module in<tt class="literal">@INC</tt> (using the <span class="option">+trace</span> optionmakes it clear that an unavailable module was not found in<tt class="literal">@INC</tt>). Now that we have some context,let's take a look at the <span class="option">proxy</span>option. In the above example, <tt class="literal">proxy</tt> is the URLwhere the SOAP server lives. When SOAP::Lite sees the<span class="option">proxy</span> option, it loads the appropriate module basedon what you request.</p><a name="perlnut2-CHP-14-SECT-2.1" /><div class="sect2"><h3 class="sect2">14.2.1. SOAP::Lite Methods</h3><p>All methods that <tt class="literal">SOAP::Lite</tt><a name="INDEX-1871" />provides can be used for setting and retrieving values. If youprovide no parameters, you will get the current value, and ifparameters are provided, a new value will be assigned to the object,and the method in question will return the current object (if notstated otherwise). This is suitable for stacking these calls. Forexample:</p><blockquote><pre class="code"> $lite = SOAP::Lite -> uri('http://simon.fell.com/calc') -> proxy('http://soap.4s4c.com/ssss4c/soap.asp') ;</pre></blockquote><p>The order is insignificant. You may call the <tt class="literal">new()</tt><a name="INDEX-1872" />method first, but if you don't, SOAP::Lite will doit for you. Calling the <tt class="literal">new( )</tt> method explicitlygives you additional syntax:</p><blockquote><pre class="code"> $lite = SOAP::Lite-&gt;new( uri => 'http://simon.fell.com/calc', proxy => 'http://soap.4s4c.com/ssss4c/soap.asp' );</pre></blockquote><p>The SOAP::Lite methods are:</p><dl><dt><b><tt class="literal">new( )</tt></b></dt><dd>Accepts a hash with method names as keys. It will call theappropriate methods together with the passed values..</p></dd><a name="INDEX-1873" /><dt><b><tt class="literal">transport( )</tt></b></dt><dd>Provides access to the <em class="emphasis">SOAP/Transport</em> object.You probably shouldn't play with <tt class="literal">transport()</tt>, since the object will be created for you.</p></dd><a name="INDEX-1874" /><dt><b><tt class="literal">serializer( )</tt></b></dt><dd>Provides access to the <em class="emphasis">SOAP/Serialization</em>object. You probably shouldn't play with<tt class="literal">serializer( )</tt>, since the object will be createdfor you.</p></dd><a name="INDEX-1875" /><dt><b><tt class="literal">endpoint( )</tt> </b></dt><dd>Lets you specify an endpoint without changing/loading the protocolmodule. This is useful for switching endpoints without switchingprotocols. You should call <tt class="literal">proxy( )</tt> first. Nochecks for protocol equivalence will be made.</p></dd><a name="INDEX-1876" /><dt><b><tt class="literal">outputxml( )</tt> </b></dt><dd>If true, all methods will return raw XML code, which you can thenparse with XML::Parser or a similar module.</p></dd><a name="INDEX-1877" /><dt><b><tt class="literal">autotype( )</tt> </b></dt><dd>Specifies whether the serializer will try to enable autotyping.Default is true.</p></dd><a name="INDEX-1878" /><dt><b><tt class="literal">readable( )</tt> </b></dt><dd>Specifies the format for the generated XML code, adding carriagereturns and indentation for readability.</p></dd><a name="INDEX-1879" /><dt><b><tt class="literal">namespace( )</tt></b></dt><dd>Specifies the default namespace for generated envelopes(<tt class="literal">SOAP-ENV</tt> by default).</p></dd><a name="INDEX-1880" /><dt><b><tt class="literal">encodingspace( )</tt> </b></dt><dd>Specifies the default encoding namespace for generated envelopes(<tt class="literal">SOAP-ENC</tt> by default).</p></dd><a name="INDEX-1881" /><dt><b><tt class="literal">encoding( )</tt> </b></dt><dd>Specifies the encoding for generated envelopes(<tt class="literal">UTF-8</tt> by default).</p></dd><a name="INDEX-1882" /><dt><b><tt class="literal">typelookup( )</tt> </b></dt><dd>Gives you access to the <tt class="literal">typelookup</tt> table used forautotyping.</p></dd><a name="INDEX-1883" /><dt><b><tt class="literal">multirefinplace( )</tt> </b></dt><dd>Determines whether the serializer should put values formultireferences in the first occurrence of the reference. Default isfalse.</p></dd><a name="INDEX-1884" /><dt><b><tt class="literal">on_action( )</tt></b></dt><dd>Specifies a handler (either globally or locally) to be triggered foran <tt class="literal">on_action</tt> event. The default handler is<tt class="literal">uri#method</tt>.</p></dd><a name="INDEX-1885" /><dt><b><tt class="literal">on_fault( )</tt> </b></dt><dd>Specifies a handler (either globally or locally) to be triggered foran <tt class="literal">on_fault</tt> event. The default behavior will dieon a transport error and do nothing on other error conditions.</p></dd><a name="INDEX-1886" /><dt><b><tt class="literal">on_debug( )</tt> </b></dt><dd>Globally specifies a handler to be triggered for an<tt class="literal">on_debug</tt> event. Default behavior is to do nothing,since moving the <tt class="literal">+trace/+debug</tt> options ispreferred.</p></dd><a name="INDEX-1887" /><dt><b><tt class="literal">on_nonserialized( )</tt> </b></dt><dd>Specifies a handler to be triggered for an<tt class="literal">on_nonserialized</tt> event. The default behavior is toproduce a warning, if warnings are enabled for these events.</p></dd><a name="INDEX-1888" /><dt><b><tt class="literal">call( )</tt> </b></dt><dd>Provides an alternative interface for remote method calls, withadditional options:</p><dl><dt><i><em class="emphasis">Prefixed method</em> </i></dt><dd>If you want to specify a prefix for generatedmethod's element, try:</p><blockquote><pre class="code">call('myprefix:method' => @parameters)</pre></blockquote></dd><dt><i><em class="emphasis">Access to any method</em> </i></dt><dd>To access remote procedures that have the same names as methods ofthe SOAP::Lite object:</p><blockquote><pre class="code">call(new => @parameters) </pre></blockquote></dd><dt><i><em class="emphasis">Implementation of OO interface</em> </i></dt><dd>To use the object-oriented interface:</p><blockquote><pre class="code"># You should specify uri( )my $soap = SOAP::Lite -> uri('http://my.own.site/CLASS') # ..... other parameters;my $obj = $soap->call(new => @parameters)->result;print $soap->call(method => $obj)->result;</pre></blockquote></dd><dt><i><em class="emphasis">Ability to set method's attributes</em> </i></dt><dd>To specify attributes for a method element:</p><blockquote><pre class="code">call(SOAP::Data->name('method')->attr({xmlns => 'mynamespace'}) => @parameters)</pre></blockquote></dd></dl></dd><a name="INDEX-1889" /><dt><b><tt class="literal">self( )</tt> </b></dt><dd>Returns the object reference to <em class="emphasis">global</em> defaultobject specified with the <tt class="literal">use SOAP::Lite</tt><tt class="literal">..</tt>. interface. For example:</p><blockquote><pre class="code">my $proxy = 'http://www.some.server';my $soap - SOAP::Lite->proxy($proxy);print $soap->self->proxy;</pre></blockquote><p>This prints <em class="emphasis">http://www.some.server</em>.</p></dd><a name="INDEX-1890" /><dt><b><tt class="literal">dispatch_from( )</tt> </b></dt><dd>The same as <tt class="literal">autodispatch</tt>, butdoesn't install the<tt class="literal">UNIVERSAL::AUTOLOAD</tt> handler. It installs only theAUTOLOAD handlers in specified classes.<a name="INDEX-1891" /><a name="INDEX-1892" /></p></dd></dl></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch14_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch14_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">14. SOAP</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">14.3. SOAP::Data</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -