SOAP-Lite

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

          </soap:Envelope>

    use_prefix(boolean)
        Deprecated. Use the "ns()" and "default_ns" methods described above.

        Shortcut for "serializer->use_prefix()". This lets you turn on/off
        the use of a namespace prefix for the children of the /Envelope/Body
        element. Default is 'true'.

        When use_prefix is set to 'true', serialized XML will look like
        this:

          <SOAP-ENV:Envelope ...attributes skipped>
            <SOAP-ENV:Body>
              <namesp1:mymethod xmlns:namesp1="urn:MyURI" />
            </SOAP-ENV:Body>
          </SOAP-ENV:Envelope>

        When use_prefix is set to 'false', serialized XML will look like
        this:

          <SOAP-ENV:Envelope ...attributes skipped>
            <SOAP-ENV:Body>
              <mymethod xmlns="urn:MyURI" />
            </SOAP-ENV:Body>
          </SOAP-ENV:Envelope>

        Some .NET web services have been reported to require this XML
        namespace idiom.

    soapversion(optional value)
            $client->soapversion('1.2');

        If no parameter is given, returns the current version of SOAP that
        is being used by the client object to encode requests. If a
        parameter is given, the method attempts to set that as the version
        of SOAP being used.

        The value should be either 1.1 or 1.2.

    envprefix(QName)
            $client->envprefix('env');

        This method is a shortcut for:

            $client->serializer->envprefix(QName);

        Gets or sets the namespace prefix for the SOAP namespace. The
        default is SOAP.

        The prefix itself has no meaning, but applications may wish to chose
        one explicitly to denote different versions of SOAP or the like.

    encprefix(QName)
            $client->encprefix('enc');

        This method is a shortcut for:

            $client->serializer->encprefix(QName);

        Gets or sets the namespace prefix for the encoding rules namespace.
        The default value is SOAP-ENC.

    While it may seem to be an unnecessary operation to set a value that
    isn't relevant to the message, such as the namespace labels for the
    envelope and encoding URNs, the ability to set these labels explicitly
    can prove to be a great aid in distinguishing and debugging messages on
    the server side of operations.

    encoding(encoding URN)
            $client->encoding($soap_12_encoding_URN);

        This method is a shortcut for:

            $client->serializer->encoding(args);

        Where the earlier method dealt with the label used for the
        attributes related to the SOAP encoding scheme, this method actually
        sets the URN to be specified as the encoding scheme for the message.
        The default is to specify the encoding for SOAP 1.1, so this is
        handy for applications that need to encode according to SOAP 1.2
        rules.

    typelookup
            $client->typelookup;

        This method is a shortcut for:

            $client->serializer->typelookup;

        Gives the application access to the type-lookup table from the
        serializer object. See the section on SOAP::Serializer.

    uri(service specifier)
        Deprecated - the "uri" subroutine is deprecated in order to provide
        a more intuitive naming scheme for subroutines that set namespaces.
        In the future, you will be required to use either the "ns()" or
        "default_ns()" subroutines instead of "uri()".

            $client->uri($service_uri);

        This method is a shortcut for:

            $client->serializer->uri(service);

        The URI associated with this accessor on a client object is the
        service-specifier for the request, often encoded for HTTP-based
        requests as the SOAPAction header. While the names may seem
        confusing, this method doesn't specify the endpoint itself. In most
        circumstances, the "uri" refers to the namespace used for the
        request.

        Often times, the value may look like a valid URL. Despite this, it
        doesn't have to point to an existing resource (and often doesn't).
        This method sets and retrieves this value from the object. Note that
        no transport code is triggered by this because it has no direct
        effect on the transport of the object.

    multirefinplace(boolean)
            $client->multirefinplace(1);

        This method is a shortcut for:

            $client->serializer->multirefinplace(boolean);

        Controls how the serializer handles values that have multiple
        references to them. Recall from previous SOAP chapters that a value
        may be tagged with an identifier, then referred to in several
        places. When this is the case for a value, the serializer defaults
        to putting the data element towards the top of the message, right
        after the opening tag of the method-specification. It is serialized
        as a standalone entity with an ID that is then referenced at the
        relevant places later on. If this method is used to set a true
        value, the behavior is different. When the multirefinplace attribute
        is true, the data is serialized at the first place that references
        it, rather than as a separate element higher up in the body. This is
        more compact but may be harder to read or trace in a debugging
        environment.

    parts( ARRAY )
        Used to specify an array of MIME::Entity's to be attached to the
        transmitted SOAP message. Attachments that are returned in a
        response can be accessed by "SOAP::SOM::parts()".

    self
            $ref = SOAP::Lite->self;

        Returns an object reference to the default global object the
        "SOAP::Lite" package maintains. This is the object that processes
        many of the arguments when provided on the use line.

    The following method isn't an accessor style of method but neither does
    it fit with the group that immediately follows it:

    call(arguments)
            $client->call($method => @arguments);

        As has been illustrated in previous chapters, the "SOAP::Lite"
        client objects can manage remote calls with auto-dispatching using
        some of Perl's more elaborate features. call is used when the
        application wants a greater degree of control over the details of
        the call itself. The method may be built up from a SOAP::Data
        object, so as to allow full control over the namespace associated
        with the tag, as well as other attributes like encoding. This is
        also important for calling methods that contain characters not
        allowable in Perl function names, such as A.B.C.

    The next four methods used in the "SOAP::Lite" class are geared towards
    handling the types of events than can occur during the message
    lifecycle. Each of these sets up a callback for the event in question:

    on_action(callback)
            $client->on_action(sub { qq("$_[0]") });

        Triggered when the transport object sets up the SOAPAction header
        for an HTTP-based call. The default is to set the header to the
        string, uri#method, in which URI is the value set by the uri method
        described earlier, and method is the name of the method being
        called. When called, the routine referenced (or the closure, if
        specified as in the example) is given two arguments, uri and method,
        in that order.

        .NET web services usually expect "/" as separator for "uri" and
        "method". To change SOAP::Lite's behaviour to use uri/method as
        SOAPAction header, use the following code:

            $client->on_action( sub { join '/', @_ } );
        =item on_fault(callback)

            $client->on_fault(sub { popup_dialog($_[1]) });

        Triggered when a method call results in a fault response from the
        server. When it is called, the argument list is first the client
        object itself, followed by the object that encapsulates the fault.
        In the example, the fault object is passed (without the client
        object) to a hypothetical GUI function that presents an error dialog
        with the text of fault extracted from the object (which is covered
        shortly under the SOAP::SOM methods).

    on_nonserialized(callback)
            $client->on_nonserialized(sub { die "$_[0]?!?" });

        Occasionally, the serializer may be given data it can't turn into
        SOAP-savvy XML; for example, if a program bug results in a code
        reference or something similar being passed in as a parameter to
        method call. When that happens, this callback is activated, with one
        argument. That argument is the data item that could not be
        understood. It will be the only argument. If the routine returns,
        the return value is pasted into the message as the serialization.
        Generally, an error is in order, and this callback allows for
        control over signaling that error.

    on_debug(callback)
            $client->on_debug(sub { print @_ });

        Deprecated. Use the global +debug and +trace facilities described in
        SOAP::Trace

        Note that this method will not work as expected: Instead of
        affecting the debugging behaviour of the object called on, it will
        globally affect the debugging behaviour for all objects of that
        class.

WRITING A SOAP CLIENT
    This chapter guides you to writing a SOAP client by example.

    The SOAP service to be accessed is a simple variation of the well-known
    hello world program. It accepts two parameters, a name and a given name,
    and returns "Hello $given_name $name".

    We will use "Martin Kutter" as the name for the call, so all variants
    will print the following message on success:

     Hello Martin Kutter!

  SOAP message styles
    There are three common (and one less common) variants of SOAP messages.

    These address the message style (positional parameters vs. specified
    message documents) and encoding (as-is vs. typed).

    The different message styles are:

    *   rpc/encoded

        Typed, positional parameters. Widely used in scripting languages.
        The type of the arguments is included in the message. Arrays and the
        like may be encoded using SOAP encoding rules (or others).

    *   rpc/literal

        As-is, positional parameters. The type of arguments is defined by
        some pre-exchanged interface definition.

    *   document/encoded

        Specified message with typed elements. Rarely used.

    *   document/literal

        Specified message with as-is elements. The message specification and
        element types are defined by some pre-exchanged interface
        definition.

    As of 2008, document/literal has become the predominant SOAP message
    variant. rpc/literal and rpc/encoded are still in use, mainly with
    scripting languages, while document/encoded is hardly used at all.

    You will see clients for the rpc/encoded and document/literal SOAP
    variants in this section.

  Example implementations
   RPC/ENCODED
    Rpc/encoded is most popular with scripting languages like perl, php and
    python without the use of a WSDL. Usual method descriptions look like
    this:

     Method: sayHello(string, string)
     Parameters:
        name: string
        givenName: string

    Such a description usually means that you can call a method named
    "sayHello" with two positional parameters, "name" and "givenName", which
    both are strings.

    The message corresponding to this description looks somewhat like this:

     <sayHello xmlns="urn:HelloWorld">
       <s-gensym01 xsi:type="xsd:string">Kutter</s-gensym01>
       <s-gensym02 xsi:type="xsd:string">Martin</s-gensym02>
     </sayHello>

    Any XML tag names may be used instead of the "s-gensym01" stuff -
    parameters are positional, the tag names have no meaning.

    A client producing such a call is implemented like this:

     use SOAP::Lite;
     my $soap = SOAP::Lite->new( proxy => 'http://localhost:81/soap-wsdl-test/helloworld.pl');
     $soap->default_ns('urn:HelloWorld');
     my $som = $soap->call('sayHello', 'Kutter', 'Martin');
     die $som->faultstring if ($som->fault);
     print $som->result, "\n";

    You can of course use a one-liner, too...

    Sometimes, rpc/encoded interfaces are described with WSDL definitions. A
    WSDL accepting "named" parameters with rpc/encoded looks like this:

     <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
       xmlns:s="http://www.w3.org/2001/XMLSchema"
       xmlns:s0="urn:HelloWorld"
       targetNamespace="urn:HelloWorld"
       xmlns="http://schemas.xmlsoap.org/wsdl/">
       <types>
         <s:schema targetNamespace="urn:HelloWorld">
         </s:schema>
       </types>
       <message name="sayHello">
         <part name="name" type="s:string" />
         <part name="givenName" type="s:string" />
       </message>
       <message name="sayHelloResponse">
         <part name="sayHelloResult" type="s:string" />
       </message>

       <portType name="Service1Soap">
         <operation name="sayHello">
           <input message="s0:sayHello" />
           <output message="s0:sayHelloResponse" />
         </operation>
       </portType>

       <binding name="Service1Soap" type="s0:Service1Soap">
         <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
             style="rpc" />
         <operation name="sayHello">
           <soap:operation soapAction="urn:HelloWorld#sayHello"/>
           <input>
             <soap:body use="encoded"
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
           </input>
           <output>
             <soap:body use="encoded"
               encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
           </output>
         </operation>
       </binding>
       <service name="HelloWorld">
         <port name="HelloWorldSoap" binding="s0:Service1Soap">
           <soap:address location="http://localhost:81/soap-wsdl-test/helloworld.pl" />
         </port>
       </service>
     </definitions>

    The message corresponding to this schema looks like this:

     <sayHello xmlns="urn:HelloWorld">
       <name xsi:type="xsd:string">Kutter</name>
       <givenName xsi:type="xsd:string">Martin</givenName>
     </sayHello>

    A web service client using this schema looks like this:

     use SOAP::Lite;
     my $soap = SOAP::Lite->service("file:say_hello_rpcenc.wsdl");
     eval { my $result = $soap->sayHello('Kutter', 'Martin'); };
     if ($@) {
         die $@;
     }
     print $som->result();

    You may of course also use the following one-liner:

     perl -MSOAP::Lite -e 'print SOAP::Lite->service("file:say_hello_rpcenc.wsdl")\
       ->sayHello('Kutter', 'Martin'), "\n";'

    A web service client (without a service description) looks like this.

     use SOAP::Lite;
     my $soap = SOAP::Lite->new( proxy => 'http://localhost:81/soap-wsdl-test/helloworld.pl');
     $soap->default_ns('urn:HelloWorld');
     my $som = $soap->call('sayHello',
        SOAP::Data->name('name')->value('Kutter'),
        SOAP::Data->name('givenName')->value('Martin')
     );
     die $som->faultstring if ($som->fault);
     print $som->result, "\n";

   RPC/LITERAL
    SOAP web services using the document/literal message encoding are
    usually described by some Web Service Definition. Our web service has
    the following WSDL description:

     <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
       xmlns:s="http://www.w3.org/2001/XMLSchema"
       xmlns:s0="urn:HelloWorld"
       targetNamespace="urn:HelloWorld"
       xmlns="http://schemas.xmlsoap.org/wsdl/">
       <types>
         <s:schema targetNamespace="urn:HelloWorld">
           <s:complexType name="sayHello">
             <s:sequence>
               <s:element minOccurs="0" maxOccurs="1" name="name"
                  type="s:string" />
               <s:element minOccurs="0" maxOccurs="1" name="givenName"
                  type="s:string" nillable="1" />
             </s:sequence>
           </s:complexType>

           <s:complexType name="sayHelloResponse">
             <s:sequence>
               <s:element minOccurs="0" maxOccurs="1" name="sayHelloResult"
                  type="s:string" />
             </s:sequence>
           </s:complexType>
         </s:schema>
       </types>
       <message name="sayHello">
         <part name="parameters" type="s0:sayHello" />
       </message>
       <message name="sayHelloResponse">
         <part name="parameters" type="s0:sayHelloResponse" />
       </message>

       <portType name="Service1Soap">
         <operation name="sayHello">
           <input message="s0:sayHello" />
           <output message="s0:sayHelloResponse" />
         </operation>
       </portType>

       <binding name="Service1Soap" type="s0:Service1Soap">
         <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
             style="rpc" />
         <operation name="sayHello">
           <soap:operation soapAction="urn:HelloWorld#sayHello"/>
           <input>
             <soap:body use="literal" namespace="urn:HelloWorld"/>
           </input>
           <output>
             <soap:body use="literal" namespace="urn:HelloWorld"/>
           </output>
         </operation>
       </binding>
       <service name="HelloWorld">
         <port name="HelloWorldSoap" binding="s0:Service1Soap">
           <soap:address location="http://localhost:80//helloworld.pl" />
         </port>
       </service>
      </definitions>

    The XML message (inside the SOAP Envelope) look like this:

     <ns0:sayHello xmlns:ns0="urn:HelloWorld">
        <parameters>
          <name>Kutter</name>
          <givenName>Martin</givenName>
        </parameters>
     </ns0:sayHello>

     <sayHelloResponse xmlns:ns0="urn:HelloWorld">
        <parameters>
            <sayHelloResult>Hello Martin Kutter!</sayHelloResult>
        </parameters>
     </sayHelloResponse>

    This is the SOAP::Lite implementation for the web service client:

     use SOAP::Lite +trace;
     my $soap = SOAP::Lite->new( proxy => 'http://localhost:80/helloworld.pl');

     $soap->on_action( sub { "urn:HelloWorld#sayHello" });
     $soap->autotype(0)->readable(1);
     $soap->default_ns('urn:HelloWorld');

     my $som = $soap->call('sayHello', SOAP::Data->name('parameters')->value(
        \SOAP::Data->value([
            SOAP::Data->name('name')->value( 'Kutter' ),
            SOAP::Data->name('givenName')->value('Martin'),
        ]))
    );

     die $som->fault->{ faultstring } if ($som->fault);
     print $som->result, "\n";

   DOCUMENT/LITERAL
    SOAP web services using the document/literal message encoding are
    usually described by some Web Service Definition. Our web service has
    the following WSDL description:

     <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
        xmlns:s="http://www.w3.org/2001/XMLSchema"
        xmlns:s0="urn:HelloWorld"
        targetNamespace="urn:HelloWorld"
        xmlns="http://schemas.xmlsoap.org/wsdl/">
       <types>
         <s:schema targetNamespace="urn:HelloWorld">
           <s:element name="sayHello">
             <s:complexType>
               <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" name="name" type="s:string" />
                   <s:element minOccurs="0" maxOccurs="1" name="givenName" type="s:string" nillable="1" />
               </s:sequence>
              </s:complexType>
            </s:element>

            <s:element name="sayHelloResponse">
              <s:complexType>
                <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" name="sayHelloResult" type="s:string" />
                </s:sequence>
            </s:complexType>
          </s:element>
        </types>
        <message name="sayHelloSoapIn">
          <part name="parameters" element="s0:sayHello" />
        </message>
        <message name="sayHelloSoapOut">
          <part name="parameters" element="s0:sayHelloResponse" />
        </message>

        <portType name="Service1Soap">
          <operation name="sayHello">
            <input message="s0:sayHelloSoapIn" />
            <output message="s0:sayHelloSoapOut" />
          </operation>
        </portType>

        <binding name="Service1Soap" type="s0:Service1Soap">
          <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
              style="document" />
          <operation name="sayHello">
            <soap:operation soapAction="urn:HelloWorld#sayHello"/>
            <input>
              <soap:body use="literal" />
            </input>
            <output>
              <soap:body use="literal" />
            </output>
          </operation>
        </binding>
        <service name="HelloWorld">
          <port name="HelloWorldSoap" binding="s0:Service1Soap">
            <soap:address location="http://localhost:80//helloworld.pl" />
          </port>
        </service>
     </definitions>

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.595 second using v1.00-cache-2.02-grep-82fe00e-cpan-9f2165ba459b )