Develop webservice in PHP

25
Develop a Web service in 5 minutes using nusoap library Sanil S Technology Evangelist http:// www.iamatechie.com

description

Develop a webservice in php using nusoap library.

Transcript of Develop webservice in PHP

Page 1: Develop webservice in PHP

Develop a Web service in 5 minutes using nusoap library

Sanil S

Technology Evangelist

http://www.iamatechie.com

Page 2: Develop webservice in PHP

My portfolio

Worked as Chief Technology Architect for MobMe

➄ Designed & developed “fastalerts” (Most popular web 2.0 Alert solutions)

➄ Architect design for simple dialer solution for Asterisk calling interface

➄ Asterisk based voice solutions➄ Mobshare mobile content sharing

platform (Acted as a role of developer)➄ Chief Architect IVR solutions for

Vodafone

Page 3: Develop webservice in PHP

Who is this talk for?

➲ PHP developers with moderate degree of expertise in PHP

➲ PHP developers wanting to implement SOAP based webservice

➲ PHP developers who think webservices is rocket science

➲ It is not intended for ASP.NET developers

Page 4: Develop webservice in PHP

What will be covered

➲ What are web services➲ Basics of SOAP➲ Implementing a simple webservice using

NuSOAP

Page 5: Develop webservice in PHP

What is a webservice?

➲ Loosely coupled, reusable software components that semantically encapsulate discrete functionality and are distributed and programmatically accessible over standard Internet protocols

Page 6: Develop webservice in PHP

What is a webservice??

➲ Remote Procedure Calling protocol that works over HTTP.

Page 7: Develop webservice in PHP

Where to use Webservices?

➲ Retrieve information dynamically over web Service integrations Price comparisons Hotel bookings

➲ Web applications requiring integration with diverse programming languages

Page 8: Develop webservice in PHP

Why use PHP

➲ Already a very popular for web development➲ XML support➲ CURL support➲ OOP➲ Potential SOAP extension

Page 9: Develop webservice in PHP

SOAP

➲ Simple Object Access Protocol➲ HTTP + XML = SOAP

Page 10: Develop webservice in PHP

SOAP Message

➲ Simple Object Access Protocol➲ HTTP + XML = SOAP

Page 11: Develop webservice in PHP

SOAP RequestPOST /examples HTTP/1.1User-Agent: Radio UserLand/7.0 (WinNT)Host: localhost:81Content-Type: text/xml; charset=utf-8Content-length: 474SOAPAction: "/examples"

<?xml version="1.0"?><SOAP-ENV:Envelope

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">

<SOAP-ENV:Body> <m:getCityName xmlns:m="http://www.soapware.org/"> <statenum xsi:type="xsd:int">691003</statenum> </ m:getCityName > </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Soap envelope

Soap body

Http Request header

Page 12: Develop webservice in PHP

SOAP ResponseHTTP/1.1 200 OKConnection: closeContent-Length: 499Content-Type: text/xml; charset=utf-8Date: Wed, 28 Mar 2001 05:05:04 GMTServer: UserLand Frontier/7.0-WinNT

<?xml version="1.0"?><SOAP-ENV:Envelope

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">

<SOAP-ENV:Body> < m:getCityNameResponse xmlns:m="http://www.soapware.org/"> <Result xsi:type="xsd:string">Kollam</Result> </ m:getCityNameResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Soap envelope

Soap body

Http Response header

Page 13: Develop webservice in PHP

SOAP FaultHTTP/1.1 500 Server ErrorConnection: closeContent-Length: 511Content-Type: text/xml; charset=utf-8Date: Wed, 28 Mar 2001 05:06:32 GMTServer: UserLand Frontier/7.0-WinNT

<?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/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">

<SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Client</faultcode> <faultstring>Can't call getCityName because there are too many

parameters.</faultstring> </SOAP-ENV:Fault> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Http Response header

Page 14: Develop webservice in PHP

What we need in short

➲ XML output is not always simplest to put in PHP code

➲ The XML output is repetitive➲ Most of us are lazy➲ Less work is better

➲ In-Short what is needed is a class which abstracts the SOAP messages for us

Page 15: Develop webservice in PHP

NuSOAP Toolkit

➲ Several PHP toolkits available for SOAP➲ NuSOAP usage is simple and efficient➲ Object Oriented ...➲ URL - http://dietrich.ganx4.com/nusoap/➲ Author - Dietrich Ayala➲ Has support for WSDL generation as well

Page 16: Develop webservice in PHP

A PHP Function// Return the STD code for the City.

function getCityName ($pincode){

$cityNames = array(‘691003’ => ‘Kollam’, ‘691235’ => ‘Ernakulam’, ‘6945678’ => ‘Trivandrum’);

return $cityNames[‘$pincode’];

}

Page 17: Develop webservice in PHP

SOAP Server

require_once('nusoap.php');

$server = new soap_server;

$server->register(getCityName');

$server->service ($_SERVER['HTTP_RAW_POST_DATA']);

exit();

Page 18: Develop webservice in PHP

SOAP Client

require_once('nusoap.php');

$param = array(‘pincode'=>’691003’);

$client = new soapclient ('http://localhost/service.php?wsdl');

$response = $client->call(‘getCityName', $param);

$response will now have the City name for the pincode passed as parameter... ...

Page 19: Develop webservice in PHP

What is missing?

➲ What if you don't pass a Pincode ?➲ What if you don't get a result?➲ What if....➲ SOAP Fault generation

Page 20: Develop webservice in PHP

PHP Function revisted/ Return the STD code for the City.

function getCityName ($pincode){

global $db

if ($pincode == '') {

/* Return a SOAP fault indicating a blank pincode */

return new soap_fault(

'Client', '',

'Must supply a pincode',''

);

}

$cityNames = array(‘691003’ => ‘Kollam’, ‘691235’ => ‘Ernakulam’, ‘6945678’ => ‘Trivandrum’);

return $cityNames[‘$pincode’];

}

Page 21: Develop webservice in PHP

SOAP Client revistedrequire_once('nusoap.php');

$param = array(‘pincode'=>’691003’);

$client = $client = new soapclient ('http://localhost/service.php?wsdl');

$response = $client->call('getCityName', $param);

if($client->fault){echo "FAULT: <p>Code: {$client->faultcode} <br />"; echo "String: {$client->faultstring} </p>";

} else{

echo $response; }

Page 22: Develop webservice in PHP

Some considerations

➲ SOAP transactions/session ➲ SOAP authentication and security

Page 23: Develop webservice in PHP

SOAP Resources

➲ Other PHP SOAP implementations PHP-SOAP Extension Activestate SWSAPI for PHP Manuel Lemos SOAP class

Page 24: Develop webservice in PHP

SOAP Resources

➲ SOAP and web services reference sites:

http://www.xml.com/pub/a/2001/04/04/webservices/ XML.com: A Web Services Primer

http://www.w3c.org/tr/soap - SOAP 1.1 specification

http://www-106.ibm.com/developerworks/webservices/ - IBM developerWorks Web Services Zone

Page 25: Develop webservice in PHP

Thank you.