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)
the soapy (wash) cycle (recap)
the soapy (message) view
PHP comes with a “standard” SOAP library tutorial at The PEAR library has a SOAP module probably the most updated, but not well documented The NuSOAP library <<< WE USE THIS! php soap:
Written in PHP (no new modules to install or configure) Simple object-oriented interface Automatically generated WSDL php NuSOAP lib characteristics:
SOAP Server SOAP Client Using WSDL Error Checking Complex Types php NuSOAP lib features:
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 );
php soap : Server
php nusoap : Server
php nusoap : Client // Pull in the NuSOAP code require_once('lib/nusoap.php'); // Create the client instance $client = new nusoap_client(' 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
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). ' ';
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 ' '; }
php nusoap : envelope for request
php nusoap : envelope for response
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");
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') ) );
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; }
php nusoap : COMPLEX TYPE function getStudentViaMatriks($matriks) { $student = new Student("rbk", $matriks, 12, "male"); return $student; }
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 );
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; }