160 likes | 429 Views
XML in the real world (2). SOAP. What is SOAP?. SOAP stands for Simple Object Access Protocol SOAP is a communication protocol SOAP is for communication between applications SOAP is a format for sending messages SOAP is designed to communicate via Internet
E N D
What is SOAP? • SOAP stands for Simple Object Access Protocol • SOAP is a communication protocol • SOAP is for communication between applications • SOAP is a format for sending messages • SOAP is designed to communicate via Internet • SOAP is platform independent • SOAP is language independent • SOAP is based on XML • SOAP is simple and extensible • SOAP allows you to get around firewalls • SOAP will be developed as a W3C standard (from W3Schools)
SOAP • Corresponds to Remote Procedure Call (RPC) in Unix • Corresponds to Active X (Windows) • Uses http: protocol • Defined and coded in XML • We need to know something about objects and messages
Object Orientation • A reaction to old programming practices where instructions and data could be confused • Developed via parameterised function calls, data hiding, global and local variables • An object is an instantiation of a class (e.g. a real trumpet is an instantiation of a complete description of the trumpet class) • It only responds to messages sent by other objects – no direct access is permitted • An object has certain attributes and can carry out one or more methods • The overhead is worth it in terms of robust and re-usable objects (e.g. printer drivers)
SOAP components • SOAP ENVELOPE: Defines a framework for expressing what is in a message and who should handle it. The SOAP envelope namespace defines header and body element names and the encoding style. • SOAP ENCODING RULES: Defines a mechanism for exchanging instances of application defined data types. An encoding rule means an encoding style to know how it is applied to a specific data. • SOAP RPC: Defines a method to represent Remote Procedure Calls and responses. Soap RPC uses a request/response model for message exchanges. The request that is sent to the end point is the call and the response it sends represents the result of the call sent.
SOAP Building Blocks A SOAP message is an ordinary XML document containing the following elements: • A required Envelope element that identifies the XML document as a SOAP message • An optional Header element that contains header information • A required Body element that contains call and response information • An optional Fault element that provides information about errors that occurred while processing the message
Skeleton SOAP Message <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle=http://www.w3.org/2001/12/soap-encoding> <soap:Header> ... ... </soap:Header> <soap:Body> ... ... <soap:Fault> ... ... </soap:Fault> </soap:Body> </soap:Envelope>
SOAP request example POST /InStock HTTP/1.1 Host: www.stock.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.stock.org/stock"> <m:GetStockPrice> <m:StockName>IBM </m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope>
SOAP response example HTTP/1.1 200 OK Content-Type: application/soap; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.stock.org/stock"> <m:GetStockPriceResponse> <m:Price>34.5 </m:Price> </m:GetStockPriceResponse> </soap:Body> </soap:Envelope>
More advanced example:Return result of 7% sales tax on a £856 purchase POST /ws/taxCalc.php HTTP/1.1 SOAPAction: urn:soap-whytewolf-ca:taxcalc#taxCalc Content-Type: text/xml Content-Length: 557 User-Agent: kSOAP/1.0 Host: www.whytewolf.ca <SOAP-ENV:Envelope xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body SOAP-ENV:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/"> <taxCalc xmlns="urn:soap-whytewolf-ca:taxcalc" id="o0" SOAP-ENC:root="1"> <rate xmlns="" xsi:type="xsd:string">7</rate> <sub xmlns="" xsi:type="xsd:string">856</sub> </taxCalc> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
The result HTTP/1.1 200 OK Date: Mon, 12 Aug 2002 01:31:10 GMT Server: Apache/1.3.14 (Unix) X-Powered-By: PHP/4.0.6 Status: 200 OK Connection: Close Content-Length: 510 Content-Type: text/xml; charset=UTF-8 <?xml version="1.0"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd"> <SOAP-ENV:Body> <taxCalcResponse> <noname xsi:type="xsd:float">915.92</noname></taxCalcResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
A SOAP fault message (optional) HTTP/1.1 200 OK Date: Mon, 12 Aug 2002 01:32:12 GMT Server: Apache/1.3.14 (Unix) X-Powered-By: PHP/4.0.6 Status: 500 Internal Server Error Connection: Close Content-Length: 607 Content-Type: text/xml; charset=UTF-8 <?xml version="1.0"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd"> <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>Client</faultcode> <faultactor></faultactor> <faultstring>Must supply a non-zero tax rate.</faultstring> <faultdetail></faultdetail> </SOAP-ENV:Fault></SOAP-ENV:Body> </SOAP-ENV:Envelope>
A SOAP server in PHP • The function that returns the result: // Return the generated total for the purchasefunction taxCalc ($rate=0, $sub=0) { return (($rate / 100) * $sub) + $sub; } • The required library (nusoap):require_once('nusoap.php');$server = new soap_server;$server->register('taxCalc'); • The service must run in the background:$server->service($_SERVER['HTTP_RAW_POST_DATA']); exit(); • Finally, we could include a fault reporting routine:return new soap_fault ( 'Client', '', 'Must supply a positive, non-zero tax rate.','' );
Full example (server) <?php /* * taxCalc.php * An example PHP script which uses NuSOAP to provide a tax calculator function. * * Author Sean Campbell */ /* Include the NuSOAP library. */ require_once('nusoap.php'); /* Create a new SOAP server instance. */ $server = new soap_server; /* Register the taxCalc function for publication. */ $server->register('taxCalc'); //Calculates the total (including tax) for a purchase based on the function taxCalc ($rate, $sub) { if ($rate == '' || $rate <= 0) { // Return a SOAP fault indicating a negative or zero tax rate was transmitted. return new soap_fault( 'Client', '', 'Must supply a positive, non-zero tax rate.','' ); } if ($sub == '' || $sub <= 0) { // Return a SOAP fault indicating a negative or zero subtotal was transmitted. return new soap_fault( 'Client', '', 'Must supply a positive, non-zero subtotal.', '' ); } // Return the generated total for the purchase. return (($rate / 100) * $sub) + $sub; } // Begin the HTTP listener service and exit. $server->service($_SERVER['HTTP_RAW_POST_DATA']); exit(); ?>
Client example <html> <head> <title>Tax calculator Client</title> </head> <body> <h1>Tax calculated:</h1> <?php /* An example PHP script which uses NuSOAP to access a tax calculator function. */ Author Sean Campbell /* Include the NuSOAP library. */ require_once('nusoap.php'); /* Initialize the client's parameter list. */ $param = array ('rate' => $_GET['rate'], 'sub' => $_GET['sub'] ); /* Create a new client by providing the endpoint to the constructor. */ $client = new soapclient ( 'http://www.whytewolf.ca/ws/taxCalc.php' ); /* Call the taxCalc() function, passing the parameter list. */ $response = $client->call('taxCalc', $param); /* handle any SOAP faults. */ if ($client->fault) { echo "FAULT: <p>Code: {$client->faultcode}<br />"; echo "String: {$client->faultstring}"; } else { echo "$" . $response; } ?> </body> </html>