News, Events, Trends, Activities, Conferences and Notes to do with Web Culture, Development, New Media, Content Management, Mobile and PDA Access and Web Infrastructure
|
See Also: Home Links Personal Site Blogroll FriendFeed CV | |
Wiki Menu:Tags:
|
SOAP : Simple Object Access Protocolthe following points are taken from a presentation I did to colleagues in 2004 outlining web services, and SOAP in particular, its a contrived example and not functional but serves to illustrate some key concepts
Overview
Data TypesUses named parameters and data types are all identified through attributes using XML::Schema, e.g.
Primitive TypesSimilar set to XML-RPC primitives (int, double, string, boolean, dateTime, base64) but with the following additional types... duration, anyURI, hexBinary, byte, unsignedShort (Long/Byte/Int) And the ability to define custom types using base XML::Schema components like...
And can provide dimension and bounds limits on data values (e.g. when assembling arrays)
Compound TypesBasically structs and arrays as you'd expect and similar to XML-RPC, but using the full set of SOAP primitives
Client Example
my $userName = $q->param('user');
my $userDetails = SOAP::Lite -> uri('/MyAppPackageName') # static despatch, class name -> proxy('http://mydomain.co.nz/soap/MyScript.cgi') -> getPhoneNumber($userName); my @results = $userDetails ->paramsout; unless ($userDetails ->fault) { print "<strong>Params fetched:</strong>"; foreach my $tempVar (@results) { print "<br>$tempVar\n"; } } else { print "<h2>SOAP Error!!!</h2>" . "<br>FaultCode: $userDetails ->faultcode" . "<br>FaultString: $userDetails ->faultstring" . "<br>FaultDetail: $userDetails ->faultdetail"; }
Server Example
use SOAP::Transport::HTTP;
SOAP::Transport::HTTP::CGI -> dispatch_to('MyAppPackageName') # static dispatch to package -> handle; package MyAppPackageName; sub getPhoneNumber{ use DBI; my ($packageName, $userName) = @_; my $returnValue = "unkown"; my $dbh = DBI->connect("DBI:mysql:MyDatabaseName:localhost", "USER", "PASSWORD"); my $sql="SELECT phone FROM people WHERE username = '$userName'"; my $sth = $dbh->prepare($sql); $sth->execute or die ("SQL error occured\n"); if ($sth->rows()) { my @row=$sth->fetchrow; $returnValue = $row[0]; } return ($returnValue); }
Sample Request
POST /soap/MyScript.cgi HTTP/1.1
Host: me.mydomain.co.nz Content-Type: text/xml Content-Length: 306 <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap:Body xmlns:myns="http://mynamespace"> <myns:getPhoneNumber><user>FRED</user></myns:getPhoneNumber> </soap:Body> </soap:Envelope>
Sample Response
HTTP/1.1 200 OK
Date: Sun, 07 Sep 2003 22:06:03 GMT Server: Apache/1.3.27 (Unix) etc etc... SOAPServer: SOAP::Lite/Perl/0.55 Content-Length: 604 Content-Type: text/xml; charset=utf-8 <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <soap:Body> <myns:getPhoneNumberResponse xmlns:myns="http://mynamespace"> <s-gensym5 xsi:type="xsd:string">07 555 2313</s-gensym5> </myns:getPhoneNumberResponse> </soap:Body> </soap:Envelope>
See Also: XML Technologies | REST | Web Services | Notes Index |