Download presentation
Presentation is loading. Please wait.
Published bySharlene Owen Modified over 8 years ago
1
web services Web services today are frequently just Application Programming Interfaces (API) or web APIs that can be accessed over a network, such as the Internet, and executed on a remote system hosting the requested services. Application Programming Interfacesweb APIsInternet (Wikipedia)
2
the soapy (wash) cycle (recap)
3
the soapy (message) view
4
PHP comes with a “standard” SOAP library http://www.php.net/soap tutorial at http://devzone.zend.com/node/view/id/689http://devzone.zend.com/node/view/id/689 The PEAR library has a SOAP module http://pear.php.net/package/SOAP probably the most updated, but not well documented The NuSOAP library <<< WE USE THIS! http://sourceforge.net/projects/nusoap/ php soap:
5
Written in PHP (no new modules to install or configure) Simple object-oriented interface Automatically generated WSDL php NuSOAP lib characteristics:
6
SOAP Server SOAP Client Using WSDL Error Checking Complex Types php NuSOAP lib features:
7
php nusoap : Server // Pull in the NuSOAP code require_once('lib/nusoap.php'); // Create the server instance $server = new nusoap_server(); // Initialize WSDL support $server->configureWSDL('hellowsdl', 'urn:hellowsdl'); // Register the method to expose $server->register('hello',// method name array('name' => 'xsd:string'),// input parameters array('return' => 'xsd:string'),// output parameters 'urn:hellowsdl',// namespace 'urn:hellowsdl#hello',// soapaction 'rpc',// style 'encoded',// use 'Says hello to the caller'// documentation );
8
php soap : Server
9
php nusoap : Server
11
php nusoap : Client // Pull in the NuSOAP code require_once('lib/nusoap.php'); // Create the client instance $client = new nusoap_client('http://localhost/nusoap/hellowsdl.php?wsdl', true); // Check for an error $err = $client->getError(); if ($err) { // Display the error echo ' Constructor error '. $err. ' '; // At this point, you know the call that follows will fail } // Call the SOAP method $result = $client->call('hello', array('name' => 'Scott')); //$result = $client->call('hello'); //with error
12
php nusoap : Client // Display the request and response echo ' Request '; echo ' '. htmlspecialchars($client->request, ENT_QUOTES). ' '; echo ' Response '; echo ' '. htmlspecialchars($client->response, ENT_QUOTES). ' '; // Display the debug messages echo ' Debug '; echo ' '. htmlspecialchars($client->debug_str, ENT_QUOTES). ' ';
13
php nusoap : Client (debug) // Check for a fault if ($client->fault) { echo ' Fault '; print_r($result); echo ' '; } else { // Check for errors $err = $client->getError(); if ($err) { // Display the error echo ' Error '. $err. ' '; } else { // Display the result echo ' Result '; print_r($result); echo ' '; }
14
php nusoap : envelope for request
15
php nusoap : envelope for response
16
php nusoap : COMPLEX TYPE class Student { public $fullname; public $matriks; public $age; public $gender; public function __construct($fullname, $matriks, $age, $gender) { $this->fullname = $fullname; $this->matriks = $matriks; $this->age = $age; $this->gender = $gender; } //example to use //$student = new Student("jb", "mat007", 12, "male");
17
php nusoap : COMPLEX TYPE // Register complex type for class student $server->wsdl->addComplexType( 'Student', //name 'complexType', //class type 'struct', //associative array 'all', //compositor ??? '', //restrictionBase ??? array( //elements 'fullname' => array('name' => 'fullname', 'type' => 'xsd:string'), 'matriks' => array('name' => 'matriks', 'type' => 'xsd:string'), 'age' => array('name' => 'age', 'type' => 'xsd:int'), 'gender' => array('name' => 'gender', 'type' => 'xsd:string') ) );
18
php nusoap : COMPLEX TYPE // Register the method and its return complex type //complex type – tns:Student $server->register('getStudentViaMatriks',// method name array('matriks' => 'xsd:string'),// input parameters array('return' => 'tns:Student'), // output parameters 'urn:studentws',// namespace 'urn:studentws#getStudentViaMatriks',// soapaction 'rpc',// style 'encoded',// use 'Return a student based on matriks' // documentation ); function getStudentViaMatriks($matriks) { $student = new Student("rbk", $matriks, 12, "male"); return $student; }
19
php nusoap : COMPLEX TYPE function getStudentViaMatriks($matriks) { $student = new Student("rbk", $matriks, 12, "male"); return $student; }
20
php nusoap : Array complex type // Register the method and its return complex type //complex type – tns:Student>> xsd:Array $server->register('getStudentViaMatriks',// method name array('matriks' => 'xsd:string'),// input parameters array('return' => 'xsd:Array'), // output parameters 'urn:studentws',// namespace 'urn:studentws#getStudentViaMatriks',// soapaction 'rpc',// style 'encoded',// use 'Return a student based on matriks' // documentation );
21
php nusoap : Array complex type function getStudentsViaName($studentname) { $data = array(); $student = new Student("jb", "mat007", 12, "male"); array_push($data, $student); $student = new Student("je", "mat007", 13, "male"); array_push($data, $student); //converting array of objects into xsd:Array of tns:Student $array = json_decode(json_encode($data), true); return $array; }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.