SlideShare ist ein Scribd-Unternehmen logo
1 von 39
Downloaden Sie, um offline zu lesen
PHP web services with the NuSOAP library 
Fulvio Corno, Dario Bonino 
e-lite Research Group 
Dipartimento di Automatica e Informatica 
Politecnico di Torino 
Torino - Italy 
http://elite.polito.it 
v. 2.1, April 2, 2009
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
2 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Goals 
Create and use Web Services using the RPC messaging model 
Being able to create a Web Service in PHP (server-side) 
Being able to call an existing Web Service from PHP code 
(client-side) 
Hiding, as much as possible, the creation of SOAP messages, 
and their decoding 
Avoid the difficulty of creating/editing WSDL files 
3 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Potential Solutions 
Php comes with a “standard” SOAP library 
http://www.php.net/soap 
tutorial at http://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 will use this) 
http://sourceforge.net/projects/nusoap/ 
4 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Limitations 
All considered libraries are easy to use when the input(s) and 
output of the web service are simple types (numbers, strings) 
Complex types are difficult to “teach” to the libraries, and they 
are not fully supported 
For practical reasons, we will try to use only xsd:string types 
5 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
6 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
NuSOAP characteristics 
Written in PHP (no new modules to install or configure) 
Simple object-oriented interface 
May work with or without WSDL files 
May automatically generate a WSDL file for the service 
7 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Installation 
Download from 
http://sourceforge.net/projects/nusoap/ 
Uncompress the file, and copy the lib directory under your 
PHP project 
In your PHP files (client or server), import the needed classes 
require_once(’lib/nusoap.php’); 
8 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
9 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Server 
Minimal SOAP server 
require_once(’lib/nusoap.php’) ; 
$server = new nusoap_server; // Create server instance 
$server->register( ’myFunction’ ) ; 
function myFunction($parameters) { 
. . . 
return $result; 
} 
// Use the request to (try to) invoke the service 
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) 
? $HTTP_RAW_POST_DATA : ’’; 
$server->service($HTTP_RAW_POST_DATA); 
10 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Server 
Step 1: create the server 
// Create the server instance 
$server = new nusoap_server; 
$server is the special variable with the SOAP server 
functionality 
11 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Server 
Step 2: identify the service function 
$server->register( ’myFunction’ ) ; 
registers a new function (SOAP “operation”) in the same web 
service 
12 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Server 
Step 3: implement the service function 
function myFunction($parameters) { 
. . . 
return $result; 
} 
function containing the implementation of the web service 
receives the SOAP input(s) directly as funtion parameter(s) 
its return value is automatically packaged as the SOAP return 
value 
13 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Server 
Step 4: execute the web service 
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) 
? $HTTP_RAW_POST_DATA : ’’; 
$server->service($HTTP_RAW_POST_DATA); 
$HTTP_RAW_POST_DATA should contain the XML SOAP 
request 
$server->service analyzes the XML, calls the function, and 
creates the XML response 
the actual web service has already been called (http) to get the 
current page 
14 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
15 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Client 
Minimal SOAP client 
require_once(’lib/nusoap.php’); 
// Create the client instance 
$client = new nusoap_client(’http://localhost/ws.php’, 
false); // false: no WSDL 
// Call the SOAP method 
$result = $client->call(’myFunction’, 
array(’param’ => ’abc’)); 
print_r($result) ; 
16 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Client 
Step 1: create the client 
$client = new nusoap_client( 
’http://localhost/ws.php’, 
false ); // false: no WSDL 
$client is the special variable with the SOAP client 
functionality 
the first parameter is the URL of the web service endpoint 
the second parameter is false: no WSDL is used 
17 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Client 
Step 2: call the web service 
$result = $client->call( 
’myFunction’, 
array(’param’ => ’abc’) ); 
the first parameter is the name of the called function 
the second parameter is an array with the list of SOAP inputs 
the array contains a list of parameter name => parameter value 
couples 
18 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
SOAP Client 
Step 3: use the result 
print_r($result) ; 
the result is available in a PHP variable 
usual PHP type conversions apply 
19 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
20 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Using WDSL 
the WSDL file should describe the web service, and in particular 
the name(s) and type(s) of input and output parameters 
in some cases, we already have a WSDL description file 
in other cases, we want the server to automatically provide a 
WSDL file describing its services 
21 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Server support for WDSL 
Support WSDL generation 
$server->configureWSDL(’demows’, 
’http://example.org/demo’) ; 
tell the NuSOAP server that we want to support WSDL 
generation 
parameters: name of the service, namespace URL 
22 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Server support for WDSL 
Declare input/output types 
$server->register( ’myFunction’, 
array("param"=>"xsd:string"), // inputs 
array("result"=>"xsd:string"), // outputs 
’http://example.org/demo’ // element namespace 
) ; 
for each function, declare types of input and output parameters 
name => type arrays 
type in XSD syntax 
23 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Self-documenting web service 
With the above server declarations, the web page 
http://localhost/ws.php may be called in 3 ways: 
1 with an http POST, by providing an XML body: the normal Web 
Service call 
2 with an http GET, with a normal browser: shows a 
human-readable description of the service 
3 with an http GET and a ?wsdl parameter: generates the WSDL 
on-the-fly and lets you download it 
24 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Client support for WDSL 
Call using WSDL information 
$client = new nusoap_client( 
’http://localhost/ws.php?wsdl’, 
true); 
the client asks for the WSDL file (that is generate on-the-fly) 
the second parameter set to true specifies that the first one is 
the address of the WSDL file (and not of the endpoint) 
25 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
26 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Error Checking - Client 
Error types 
error can either happen during the client creation or 
during the evaluation of call results 
fault can happen during the remote service call 
Service creation 
//build the nu-soap client 
$client = new nusoap_client( 
’http://localhost/ws.php?wsdl’,true); 
//check errors 
$err = $client->getError(); 
if($err) 
//write the error and do not perform the call 
echo "<strong>Error:</strong>".$err; 
27 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Error Checking - Client 
Service call 
//do the call 
$callResult = $client->call("myFunction", 
array("param"=>"abc")); 
//check fault 
if($client->fault) 
{ 
echo "<strong>Fault:</strong>". 
print_r($callResult); 
} 
28 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Error Checking - Client 
Call results 
//check fault 
if($client->fault) { 
echo "<strong>Fault:</strong>". 
print_r($callResult); 
} else { 
//check error 
$err = $client->getError(); 
if($err) 
{ 
//write the error 
echo "<strong>Error:</strong>".$err; 
} 
else { ... } //result ok 
} 
29 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
30 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Complex Types 
NuSOAP supports the definition of Complex Types, i.e., of 
complex data structures that may be exchanged by web service 
calls. Complex Types may be: 
Composed of Simple xsd types 
Composed of other Complex Types 
May include (unbound) sequences or arrays of Complex or 
Simple types 
Declared as PHP arrays or structs (associative arrays) 
31 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Complex Types: structures 
Structure types 
$server->wsdl->addComplexType( 
’Theater’, 
’complexType’, 
’struct’, 
’sequence’, 
’’, 
array( 
’idTheater’ => array( 
’name’ => ’idTheater’, ’type’ => ’xsd:int’), 
’Name’ => array( 
’name’ => ’Name’, ’type’ => ’xsd:string’), 
’Address’ => array( 
’name’ => ’Address’, ’type’ => ’xsd:string’) 
) 
); 
32 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Generated Schema for structures 
Automatically generated WSDL 
<xsd:complexType name="Theater"> 
<xsd:sequence> 
<xsd:element name="idTheater" type="xsd:int"/> 
<xsd:element name="Name" type="xsd:string"/> 
<xsd:element name="Address" type="xsd:string"/> 
</xsd:sequence> 
</xsd:complexType> 
33 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Complex Types: arrays 
Array types 
$server->wsdl->addComplexType( 
’Theaters’, 
’complexType’, 
’array’, 
’’, 
’SOAP-ENC:Array’, 
array(), 
array( 
array( 
’ref’=>’SOAP-ENC:arrayType’, 
’wsdl:arrayType’=>’tns:Theater[]’) 
), 
’tns:Theater’ 
); 
34 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Generated Schema for arrays 
Automatically generated WSDL 
<xsd:complexType name="Theaters"> 
<xsd:complexContent> 
<xsd:restriction base="SOAP-ENC:Array"> 
<xsd:attribute ref="SOAP-ENC:arrayType" 
wsdl:arrayType="tns:Theater[]"/> 
</xsd:restriction> 
</xsd:complexContent> 
</xsd:complexType> 
35 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Using Complex Types 
Service registration 
$server->register("getTheaters", 
array("param"=>"xsd:string"), // inputs 
array("result"=>"tns:Theaters"), // outputs 
’http://example.org/demo’); // element 
36 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Further information 
Software and function documentation: 
http://sourceforge.net/projects/nusoap/ 
Tutorial introduction: 
http://www.scottnichol.com/soap.htm (warning: uses 
an old version of the library) 
Slides: http://www.slideshare.net/sanil/ 
develop-webservice-in-php and 
http://www.slideshare.net/harit/ 
web-services-in-php-1094228 
37 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
Outline 
1 Web services in PHP 
2 The NuSOAP library 
SOAP Server 
SOAP Client 
Using WSDL 
Error Checking 
Complex Types 
3 License 
38 / 39 
PHP web services with the NuSOAP library
Web services in PHP The NuSOAP library License 
License 
This document is licensed under the Creative Commons 
Attribution-Noncommercial-Share Alike 3.0 Unported License. 
http://creativecommons.org/licenses/by-nc-sa/3.0/ 
39 / 39 
PHP web services with the NuSOAP library

Weitere ähnliche Inhalte

Was ist angesagt?

Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Best Practice in API Design
Best Practice in API DesignBest Practice in API Design
Best Practice in API DesignLorna Mitchell
 
Building Restful Applications Using Php
Building Restful Applications Using PhpBuilding Restful Applications Using Php
Building Restful Applications Using PhpSudheer Satyanarayana
 
The Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIThe Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIEyal Vardi
 
Restful web services with java
Restful web services with javaRestful web services with java
Restful web services with javaVinay Gopinath
 
Server-Side Programming Primer
Server-Side Programming PrimerServer-Side Programming Primer
Server-Side Programming PrimerIvano Malavolta
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WSKatrien Verbert
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API RecommendationsJeelani Shaik
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web servicesnbuddharaju
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsi krishna
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSCarol McDonald
 
Introduction to RESTful Webservices in JAVA
Introduction to RESTful Webservices  in JAVA Introduction to RESTful Webservices  in JAVA
Introduction to RESTful Webservices in JAVA psrpatnaik
 
HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 ConferenceRoger Kitain
 
RESTful Web services using JAX-RS
RESTful Web services using JAX-RSRESTful Web services using JAX-RS
RESTful Web services using JAX-RSArun Gupta
 

Was ist angesagt? (20)

Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Develop webservice in PHP
Develop webservice in PHPDevelop webservice in PHP
Develop webservice in PHP
 
Best Practice in API Design
Best Practice in API DesignBest Practice in API Design
Best Practice in API Design
 
ASP.NET WEB API
ASP.NET WEB APIASP.NET WEB API
ASP.NET WEB API
 
Building Restful Applications Using Php
Building Restful Applications Using PhpBuilding Restful Applications Using Php
Building Restful Applications Using Php
 
Excellent rest using asp.net web api
Excellent rest using asp.net web apiExcellent rest using asp.net web api
Excellent rest using asp.net web api
 
The Full Power of ASP.NET Web API
The Full Power of ASP.NET Web APIThe Full Power of ASP.NET Web API
The Full Power of ASP.NET Web API
 
Restful web services with java
Restful web services with javaRestful web services with java
Restful web services with java
 
Server-Side Programming Primer
Server-Side Programming PrimerServer-Side Programming Primer
Server-Side Programming Primer
 
Doing REST Right
Doing REST RightDoing REST Right
Doing REST Right
 
Using Java to implement SOAP Web Services: JAX-WS
Using Java to implement SOAP Web Services: JAX-WS�Using Java to implement SOAP Web Services: JAX-WS�
Using Java to implement SOAP Web Services: JAX-WS
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API Recommendations
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
SOAP-based Web Services
SOAP-based Web ServicesSOAP-based Web Services
SOAP-based Web Services
 
Introduction to RESTful Webservices in JAVA
Introduction to RESTful Webservices  in JAVA Introduction to RESTful Webservices  in JAVA
Introduction to RESTful Webservices in JAVA
 
HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 Conference
 
RESTful Web services using JAX-RS
RESTful Web services using JAX-RSRESTful Web services using JAX-RS
RESTful Web services using JAX-RS
 

Ähnlich wie PHP web services with the NuSOAP library

Web services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP libraryWeb services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP libraryFulvio Corno
 
Introduction to nu soap
Introduction to nu soapIntroduction to nu soap
Introduction to nu soapvikash_pri14
 
Create Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationCreate Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationRutul Shah
 
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docxRunning Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docxcowinhelen
 
Getting to know Laravel 5
Getting to know Laravel 5Getting to know Laravel 5
Getting to know Laravel 5Bukhori Aqid
 
Red5workshop 090619073420-phpapp02
Red5workshop 090619073420-phpapp02Red5workshop 090619073420-phpapp02
Red5workshop 090619073420-phpapp02arghya007
 
Introduction to CloudStack API
Introduction to CloudStack APIIntroduction to CloudStack API
Introduction to CloudStack APIKrunal Jain
 
Web scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannelWeb scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannelpurpleocean
 
IMPACT/myGrid Hackathon - Taverna Server as a Portal
IMPACT/myGrid Hackathon - Taverna Server as a PortalIMPACT/myGrid Hackathon - Taverna Server as a Portal
IMPACT/myGrid Hackathon - Taverna Server as a PortalIMPACT Centre of Competence
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#caohansnnuedu
 
Web service- Guest Lecture at National Wokshop
Web service- Guest Lecture at National WokshopWeb service- Guest Lecture at National Wokshop
Web service- Guest Lecture at National WokshopNishikant Taksande
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发shaokun
 

Ähnlich wie PHP web services with the NuSOAP library (20)

Web services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP libraryWeb services in PHP using the NuSOAP library
Web services in PHP using the NuSOAP library
 
Introduction to nu soap
Introduction to nu soapIntroduction to nu soap
Introduction to nu soap
 
Sinatra
SinatraSinatra
Sinatra
 
Create Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integrationCreate Home Directories on Storage Using WFA and ServiceNow integration
Create Home Directories on Storage Using WFA and ServiceNow integration
 
Pyrax talk
Pyrax talkPyrax talk
Pyrax talk
 
Soap Toolkit Dcphp
Soap Toolkit DcphpSoap Toolkit Dcphp
Soap Toolkit Dcphp
 
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docxRunning Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
Running Head IMPLEMENTING THE LIST AND SEARCH FEATURES IN THE DIS.docx
 
Getting to know Laravel 5
Getting to know Laravel 5Getting to know Laravel 5
Getting to know Laravel 5
 
Red5workshop 090619073420-phpapp02
Red5workshop 090619073420-phpapp02Red5workshop 090619073420-phpapp02
Red5workshop 090619073420-phpapp02
 
Apache - Quick reference guide
Apache - Quick reference guideApache - Quick reference guide
Apache - Quick reference guide
 
Laravel 5
Laravel 5Laravel 5
Laravel 5
 
Introduction to CloudStack API
Introduction to CloudStack APIIntroduction to CloudStack API
Introduction to CloudStack API
 
Web scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannelWeb scale infrastructures with kubernetes and flannel
Web scale infrastructures with kubernetes and flannel
 
IMPACT/myGrid Hackathon - Taverna Server as a Portal
IMPACT/myGrid Hackathon - Taverna Server as a PortalIMPACT/myGrid Hackathon - Taverna Server as a Portal
IMPACT/myGrid Hackathon - Taverna Server as a Portal
 
zLAMP
zLAMPzLAMP
zLAMP
 
Web Server and how we can design app in C#
Web Server and how we can design app  in C#Web Server and how we can design app  in C#
Web Server and how we can design app in C#
 
Web service- Guest Lecture at National Wokshop
Web service- Guest Lecture at National WokshopWeb service- Guest Lecture at National Wokshop
Web service- Guest Lecture at National Wokshop
 
Dwr
DwrDwr
Dwr
 
Rack
RackRack
Rack
 
Rails web api 开发
Rails web api 开发Rails web api 开发
Rails web api 开发
 

Kürzlich hochgeladen

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

PHP web services with the NuSOAP library

  • 1. PHP web services with the NuSOAP library Fulvio Corno, Dario Bonino e-lite Research Group Dipartimento di Automatica e Informatica Politecnico di Torino Torino - Italy http://elite.polito.it v. 2.1, April 2, 2009
  • 2. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 2 / 39 PHP web services with the NuSOAP library
  • 3. Web services in PHP The NuSOAP library License Goals Create and use Web Services using the RPC messaging model Being able to create a Web Service in PHP (server-side) Being able to call an existing Web Service from PHP code (client-side) Hiding, as much as possible, the creation of SOAP messages, and their decoding Avoid the difficulty of creating/editing WSDL files 3 / 39 PHP web services with the NuSOAP library
  • 4. Web services in PHP The NuSOAP library License Potential Solutions Php comes with a “standard” SOAP library http://www.php.net/soap tutorial at http://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 will use this) http://sourceforge.net/projects/nusoap/ 4 / 39 PHP web services with the NuSOAP library
  • 5. Web services in PHP The NuSOAP library License Limitations All considered libraries are easy to use when the input(s) and output of the web service are simple types (numbers, strings) Complex types are difficult to “teach” to the libraries, and they are not fully supported For practical reasons, we will try to use only xsd:string types 5 / 39 PHP web services with the NuSOAP library
  • 6. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 6 / 39 PHP web services with the NuSOAP library
  • 7. Web services in PHP The NuSOAP library License NuSOAP characteristics Written in PHP (no new modules to install or configure) Simple object-oriented interface May work with or without WSDL files May automatically generate a WSDL file for the service 7 / 39 PHP web services with the NuSOAP library
  • 8. Web services in PHP The NuSOAP library License Installation Download from http://sourceforge.net/projects/nusoap/ Uncompress the file, and copy the lib directory under your PHP project In your PHP files (client or server), import the needed classes require_once(’lib/nusoap.php’); 8 / 39 PHP web services with the NuSOAP library
  • 9. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 9 / 39 PHP web services with the NuSOAP library
  • 10. Web services in PHP The NuSOAP library License SOAP Server Minimal SOAP server require_once(’lib/nusoap.php’) ; $server = new nusoap_server; // Create server instance $server->register( ’myFunction’ ) ; function myFunction($parameters) { . . . return $result; } // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ’’; $server->service($HTTP_RAW_POST_DATA); 10 / 39 PHP web services with the NuSOAP library
  • 11. Web services in PHP The NuSOAP library License SOAP Server Step 1: create the server // Create the server instance $server = new nusoap_server; $server is the special variable with the SOAP server functionality 11 / 39 PHP web services with the NuSOAP library
  • 12. Web services in PHP The NuSOAP library License SOAP Server Step 2: identify the service function $server->register( ’myFunction’ ) ; registers a new function (SOAP “operation”) in the same web service 12 / 39 PHP web services with the NuSOAP library
  • 13. Web services in PHP The NuSOAP library License SOAP Server Step 3: implement the service function function myFunction($parameters) { . . . return $result; } function containing the implementation of the web service receives the SOAP input(s) directly as funtion parameter(s) its return value is automatically packaged as the SOAP return value 13 / 39 PHP web services with the NuSOAP library
  • 14. Web services in PHP The NuSOAP library License SOAP Server Step 4: execute the web service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ’’; $server->service($HTTP_RAW_POST_DATA); $HTTP_RAW_POST_DATA should contain the XML SOAP request $server->service analyzes the XML, calls the function, and creates the XML response the actual web service has already been called (http) to get the current page 14 / 39 PHP web services with the NuSOAP library
  • 15. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 15 / 39 PHP web services with the NuSOAP library
  • 16. Web services in PHP The NuSOAP library License SOAP Client Minimal SOAP client require_once(’lib/nusoap.php’); // Create the client instance $client = new nusoap_client(’http://localhost/ws.php’, false); // false: no WSDL // Call the SOAP method $result = $client->call(’myFunction’, array(’param’ => ’abc’)); print_r($result) ; 16 / 39 PHP web services with the NuSOAP library
  • 17. Web services in PHP The NuSOAP library License SOAP Client Step 1: create the client $client = new nusoap_client( ’http://localhost/ws.php’, false ); // false: no WSDL $client is the special variable with the SOAP client functionality the first parameter is the URL of the web service endpoint the second parameter is false: no WSDL is used 17 / 39 PHP web services with the NuSOAP library
  • 18. Web services in PHP The NuSOAP library License SOAP Client Step 2: call the web service $result = $client->call( ’myFunction’, array(’param’ => ’abc’) ); the first parameter is the name of the called function the second parameter is an array with the list of SOAP inputs the array contains a list of parameter name => parameter value couples 18 / 39 PHP web services with the NuSOAP library
  • 19. Web services in PHP The NuSOAP library License SOAP Client Step 3: use the result print_r($result) ; the result is available in a PHP variable usual PHP type conversions apply 19 / 39 PHP web services with the NuSOAP library
  • 20. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 20 / 39 PHP web services with the NuSOAP library
  • 21. Web services in PHP The NuSOAP library License Using WDSL the WSDL file should describe the web service, and in particular the name(s) and type(s) of input and output parameters in some cases, we already have a WSDL description file in other cases, we want the server to automatically provide a WSDL file describing its services 21 / 39 PHP web services with the NuSOAP library
  • 22. Web services in PHP The NuSOAP library License Server support for WDSL Support WSDL generation $server->configureWSDL(’demows’, ’http://example.org/demo’) ; tell the NuSOAP server that we want to support WSDL generation parameters: name of the service, namespace URL 22 / 39 PHP web services with the NuSOAP library
  • 23. Web services in PHP The NuSOAP library License Server support for WDSL Declare input/output types $server->register( ’myFunction’, array("param"=>"xsd:string"), // inputs array("result"=>"xsd:string"), // outputs ’http://example.org/demo’ // element namespace ) ; for each function, declare types of input and output parameters name => type arrays type in XSD syntax 23 / 39 PHP web services with the NuSOAP library
  • 24. Web services in PHP The NuSOAP library License Self-documenting web service With the above server declarations, the web page http://localhost/ws.php may be called in 3 ways: 1 with an http POST, by providing an XML body: the normal Web Service call 2 with an http GET, with a normal browser: shows a human-readable description of the service 3 with an http GET and a ?wsdl parameter: generates the WSDL on-the-fly and lets you download it 24 / 39 PHP web services with the NuSOAP library
  • 25. Web services in PHP The NuSOAP library License Client support for WDSL Call using WSDL information $client = new nusoap_client( ’http://localhost/ws.php?wsdl’, true); the client asks for the WSDL file (that is generate on-the-fly) the second parameter set to true specifies that the first one is the address of the WSDL file (and not of the endpoint) 25 / 39 PHP web services with the NuSOAP library
  • 26. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 26 / 39 PHP web services with the NuSOAP library
  • 27. Web services in PHP The NuSOAP library License Error Checking - Client Error types error can either happen during the client creation or during the evaluation of call results fault can happen during the remote service call Service creation //build the nu-soap client $client = new nusoap_client( ’http://localhost/ws.php?wsdl’,true); //check errors $err = $client->getError(); if($err) //write the error and do not perform the call echo "<strong>Error:</strong>".$err; 27 / 39 PHP web services with the NuSOAP library
  • 28. Web services in PHP The NuSOAP library License Error Checking - Client Service call //do the call $callResult = $client->call("myFunction", array("param"=>"abc")); //check fault if($client->fault) { echo "<strong>Fault:</strong>". print_r($callResult); } 28 / 39 PHP web services with the NuSOAP library
  • 29. Web services in PHP The NuSOAP library License Error Checking - Client Call results //check fault if($client->fault) { echo "<strong>Fault:</strong>". print_r($callResult); } else { //check error $err = $client->getError(); if($err) { //write the error echo "<strong>Error:</strong>".$err; } else { ... } //result ok } 29 / 39 PHP web services with the NuSOAP library
  • 30. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 30 / 39 PHP web services with the NuSOAP library
  • 31. Web services in PHP The NuSOAP library License Complex Types NuSOAP supports the definition of Complex Types, i.e., of complex data structures that may be exchanged by web service calls. Complex Types may be: Composed of Simple xsd types Composed of other Complex Types May include (unbound) sequences or arrays of Complex or Simple types Declared as PHP arrays or structs (associative arrays) 31 / 39 PHP web services with the NuSOAP library
  • 32. Web services in PHP The NuSOAP library License Complex Types: structures Structure types $server->wsdl->addComplexType( ’Theater’, ’complexType’, ’struct’, ’sequence’, ’’, array( ’idTheater’ => array( ’name’ => ’idTheater’, ’type’ => ’xsd:int’), ’Name’ => array( ’name’ => ’Name’, ’type’ => ’xsd:string’), ’Address’ => array( ’name’ => ’Address’, ’type’ => ’xsd:string’) ) ); 32 / 39 PHP web services with the NuSOAP library
  • 33. Web services in PHP The NuSOAP library License Generated Schema for structures Automatically generated WSDL <xsd:complexType name="Theater"> <xsd:sequence> <xsd:element name="idTheater" type="xsd:int"/> <xsd:element name="Name" type="xsd:string"/> <xsd:element name="Address" type="xsd:string"/> </xsd:sequence> </xsd:complexType> 33 / 39 PHP web services with the NuSOAP library
  • 34. Web services in PHP The NuSOAP library License Complex Types: arrays Array types $server->wsdl->addComplexType( ’Theaters’, ’complexType’, ’array’, ’’, ’SOAP-ENC:Array’, array(), array( array( ’ref’=>’SOAP-ENC:arrayType’, ’wsdl:arrayType’=>’tns:Theater[]’) ), ’tns:Theater’ ); 34 / 39 PHP web services with the NuSOAP library
  • 35. Web services in PHP The NuSOAP library License Generated Schema for arrays Automatically generated WSDL <xsd:complexType name="Theaters"> <xsd:complexContent> <xsd:restriction base="SOAP-ENC:Array"> <xsd:attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="tns:Theater[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> 35 / 39 PHP web services with the NuSOAP library
  • 36. Web services in PHP The NuSOAP library License Using Complex Types Service registration $server->register("getTheaters", array("param"=>"xsd:string"), // inputs array("result"=>"tns:Theaters"), // outputs ’http://example.org/demo’); // element 36 / 39 PHP web services with the NuSOAP library
  • 37. Web services in PHP The NuSOAP library License Further information Software and function documentation: http://sourceforge.net/projects/nusoap/ Tutorial introduction: http://www.scottnichol.com/soap.htm (warning: uses an old version of the library) Slides: http://www.slideshare.net/sanil/ develop-webservice-in-php and http://www.slideshare.net/harit/ web-services-in-php-1094228 37 / 39 PHP web services with the NuSOAP library
  • 38. Web services in PHP The NuSOAP library License Outline 1 Web services in PHP 2 The NuSOAP library SOAP Server SOAP Client Using WSDL Error Checking Complex Types 3 License 38 / 39 PHP web services with the NuSOAP library
  • 39. Web services in PHP The NuSOAP library License License This document is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. http://creativecommons.org/licenses/by-nc-sa/3.0/ 39 / 39 PHP web services with the NuSOAP library