KEMBAR78
Develop webservice in PHP | PPT
Develop a Web service in 5 minutes using nusoap library Sanil S Technology Evangelist http://www.iamatechie.com
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
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
What will be covered What are web services Basics of SOAP Implementing a simple webservice using NuSOAP
What is a webservice? Loosely coupled, reusable software components that semantically encapsulate discrete functionality and are distributed and programmatically accessible over standard Internet protocols
What is a webservice?? R emote  P rocedure  C alling protocol  that works over  HTTP .
Where to use Webservices? Retrieve information dynamically over web Service integrations Price comparisons Hotel bookings Web applications requiring integration with diverse programming languages
Why use PHP Already a very popular for web development XML support CURL support OOP Potential SOAP extension
SOAP S imple  O bject  A ccess  P rotocol HTTP + XML = SOAP
SOAP Message S imple  O bject  A ccess  P rotocol HTTP + XML = SOAP
SOAP Request POST /examples HTTP/1.1 User-Agent: Radio UserLand/7.0 (WinNT) ‏ Host: localhost:81 Content-Type: text/xml; charset=utf-8 Content-length: 474 SOAPAction: &quot;/examples&quot; <?xml version=&quot;1.0&quot;?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/1999/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/1999/XMLSchema-instance&quot;> <SOAP-ENV:Body> <m:getCityName xmlns:m=&quot;http://www.soapware.org/&quot;> <statenum xsi:type=&quot;xsd:int&quot;>691003</statenum> </ m:getCityName > </SOAP-ENV:Body> </SOAP-ENV:Envelope> Soap envelope Soap body Http Request header
SOAP Response HTTP/1.1 200 OK Connection: close Content-Length: 499 Content-Type: text/xml; charset=utf-8 Date: Wed, 28 Mar 2001 05:05:04 GMT Server: UserLand Frontier/7.0-WinNT <?xml version=&quot;1.0&quot;?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/1999/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/1999/XMLSchema-instance&quot;> <SOAP-ENV:Body> < m:getCityNameResponse xmlns:m=&quot;http://www.soapware.org/&quot;> <Result xsi:type=&quot;xsd:string&quot;>Kollam</Result> </ m:getCityNameResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> Soap envelope Soap body Http Response header
SOAP Fault HTTP/1.1 500 Server Error Connection: close Content-Length: 511 Content-Type: text/xml; charset=utf-8 Date: Wed, 28 Mar 2001 05:06:32 GMT Server: UserLand Frontier/7.0-WinNT <?xml version=&quot;1.0&quot;?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/1999/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/1999/XMLSchema-instance&quot;> <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
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
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
A PHP Function // Return the STD code for the City. function getCityName ($pincode){  $cityNames = array(‘691003’ => ‘Kollam’, ‘691235’ => ‘Ernakulam’, ‘6945678’ => ‘Trivandrum’); return $cityNames[‘$pincode’]; }
SOAP Server require_once('nusoap.php');  $server = new soap_server; $server->register( getCityName ');  $server->service ($_SERVER['HTTP_RAW_POST_DATA']); exit();
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... ...
What is missing? What if you don't pass a Pincode ? What if you don't get a result? What if.... SOAP Fault generation Loading...
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’]; }
SOAP Client revisted require_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 &quot;FAULT:  <p>Code: {$client->faultcode} <br />&quot;;  echo &quot;String: {$client->faultstring} </p>&quot;;  } else{ echo $response; }
Some considerations SOAP transactions/session  SOAP authentication and security
SOAP Resources Other PHP SOAP implementations PHP-SOAP Extension Activestate SWSAPI for PHP Manuel Lemos SOAP class
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
Thank you.

Develop webservice in PHP

  • 1.
    Develop a Webservice in 5 minutes using nusoap library Sanil S Technology Evangelist http://www.iamatechie.com
  • 2.
    My portfolio Workedas 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
  • 3.
    Who is thistalk 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
  • 4.
    What will becovered What are web services Basics of SOAP Implementing a simple webservice using NuSOAP
  • 5.
    What is awebservice? Loosely coupled, reusable software components that semantically encapsulate discrete functionality and are distributed and programmatically accessible over standard Internet protocols
  • 6.
    What is awebservice?? R emote P rocedure C alling protocol that works over HTTP .
  • 7.
    Where to useWebservices? Retrieve information dynamically over web Service integrations Price comparisons Hotel bookings Web applications requiring integration with diverse programming languages
  • 8.
    Why use PHPAlready a very popular for web development XML support CURL support OOP Potential SOAP extension
  • 9.
    SOAP S imple O bject A ccess P rotocol HTTP + XML = SOAP
  • 10.
    SOAP Message Simple O bject A ccess P rotocol HTTP + XML = SOAP
  • 11.
    SOAP Request POST/examples HTTP/1.1 User-Agent: Radio UserLand/7.0 (WinNT) ‏ Host: localhost:81 Content-Type: text/xml; charset=utf-8 Content-length: 474 SOAPAction: &quot;/examples&quot; <?xml version=&quot;1.0&quot;?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/1999/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/1999/XMLSchema-instance&quot;> <SOAP-ENV:Body> <m:getCityName xmlns:m=&quot;http://www.soapware.org/&quot;> <statenum xsi:type=&quot;xsd:int&quot;>691003</statenum> </ m:getCityName > </SOAP-ENV:Body> </SOAP-ENV:Envelope> Soap envelope Soap body Http Request header
  • 12.
    SOAP Response HTTP/1.1200 OK Connection: close Content-Length: 499 Content-Type: text/xml; charset=utf-8 Date: Wed, 28 Mar 2001 05:05:04 GMT Server: UserLand Frontier/7.0-WinNT <?xml version=&quot;1.0&quot;?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/1999/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/1999/XMLSchema-instance&quot;> <SOAP-ENV:Body> < m:getCityNameResponse xmlns:m=&quot;http://www.soapware.org/&quot;> <Result xsi:type=&quot;xsd:string&quot;>Kollam</Result> </ m:getCityNameResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> Soap envelope Soap body Http Response header
  • 13.
    SOAP Fault HTTP/1.1500 Server Error Connection: close Content-Length: 511 Content-Type: text/xml; charset=utf-8 Date: Wed, 28 Mar 2001 05:06:32 GMT Server: UserLand Frontier/7.0-WinNT <?xml version=&quot;1.0&quot;?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsd=&quot;http://www.w3.org/1999/XMLSchema&quot; xmlns:xsi=&quot;http://www.w3.org/1999/XMLSchema-instance&quot;> <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
  • 14.
    What we needin 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
  • 15.
    NuSOAP Toolkit SeveralPHP 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
  • 16.
    A PHP Function// Return the STD code for the City. function getCityName ($pincode){ $cityNames = array(‘691003’ => ‘Kollam’, ‘691235’ => ‘Ernakulam’, ‘6945678’ => ‘Trivandrum’); return $cityNames[‘$pincode’]; }
  • 17.
    SOAP Server require_once('nusoap.php'); $server = new soap_server; $server->register( getCityName '); $server->service ($_SERVER['HTTP_RAW_POST_DATA']); exit();
  • 18.
    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... ...
  • 19.
    What is missing?What if you don't pass a Pincode ? What if you don't get a result? What if.... SOAP Fault generation Loading...
  • 20.
    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’]; }
  • 21.
    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 &quot;FAULT: <p>Code: {$client->faultcode} <br />&quot;; echo &quot;String: {$client->faultstring} </p>&quot;; } else{ echo $response; }
  • 22.
    Some considerations SOAPtransactions/session SOAP authentication and security
  • 23.
    SOAP Resources OtherPHP SOAP implementations PHP-SOAP Extension Activestate SWSAPI for PHP Manuel Lemos SOAP class
  • 24.
    SOAP Resources SOAPand 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
  • 25.