Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 XML in the real world (2) SOAP

2 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)

3 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

4 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)

5 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.

6 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

7 Skeleton SOAP Message encodinghttp://www.w3.org/2001/12/soap- encoding

8 SOAP request example POST /InStock HTTP/1.1 Host: Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn IBM IBM

9 SOAP response example HTTP/ OK Content-Type: application/soap; charset=utf-8 Content-Length: nnn

10 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: 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/">

11 The result HTTP/ OK Date: Mon, 12 Aug :31:10 GMT Server: Apache/ (Unix) X-Powered-By: PHP/4.0.6 Status: 200 OK Connection: Close Content-Length: 510 Content-Type: text/xml; charset=UTF HTTP/ OK Date: Mon, 12 Aug :31:10 GMT Server: Apache/ (Unix) X-Powered-By: PHP/4.0.6 Status: 200 OK Connection: Close Content-Length: 510 Content-Type: text/xml; charset=UTF

12 A SOAP fault message (optional) HTTP/ OK Date: Mon, 12 Aug :32:12 GMT Server: Apache/ (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 Client Must supply a non-zero tax rate. HTTP/ OK Date: Mon, 12 Aug :32:12 GMT Server: Apache/ (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 Client Must supply a non-zero tax rate.

13 A SOAP server in PHP ► The function that returns the result: // Return the generated total for the purchase function 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.','' );

14 Full example (server) register('taxCalc'); //Calculates the total (including tax) for a purchase based on the function taxCalc ($rate, $sub) { if ($rate == '' || $rate 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(); ?> return (($rate / 100) * $sub) + $sub; } // Begin the HTTP listener service and exit. $server->service($_SERVER['HTTP_RAW_POST_DATA']); exit(); ?>

15 Client example Tax calculator Client Tax calculated: $_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: Code: {$client->faultcode} "; echo "String: {$client->faultstring}"; } else { echo "$". $response; } ?> Tax calculator Client Tax calculated: $_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: Code: {$client->faultcode} "; echo "String: {$client->faultstring}"; } else { echo "$". $response; } ?>


Download ppt "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."

Similar presentations


Ads by Google