Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Develop webservice in PHP

Develop a webservice in php using nusoap library.

Develop webservice in PHP

  1. 1. Develop a Web service in 5 minutes using nusoap library Sanil S Technology Evangelist http://www.iamatechie.com
  2. 2. My portfolio <ul><ul><li>Worked as Chief Technology Architect for MobMe </li></ul></ul><ul><ul><li>Designed & developed “fastalerts” (Most popular web 2.0 Alert solutions) </li></ul></ul><ul><ul><li>Architect design for simple dialer solution for Asterisk calling interface </li></ul></ul><ul><ul><li>Asterisk based voice solutions </li></ul></ul><ul><ul><li>Mobshare mobile content sharing platform (Acted as a role of developer) </li></ul></ul><ul><ul><li>Chief Architect IVR solutions for Vodafone </li></ul></ul>
  3. 3. Who is this talk for? <ul><li>PHP developers with moderate degree of expertise in PHP </li></ul><ul><li>PHP developers wanting to implement SOAP based webservice </li></ul><ul><li>PHP developers who think webservices is rocket science </li></ul><ul><li>It is not intended for ASP.NET developers </li></ul>
  4. 4. What will be covered <ul><li>What are web services </li></ul><ul><li>Basics of SOAP </li></ul><ul><li>Implementing a simple webservice using NuSOAP </li></ul>
  5. 5. What is a webservice? <ul><li>Loosely coupled, reusable software components that semantically encapsulate discrete functionality and are distributed and programmatically accessible over standard Internet protocols </li></ul>
  6. 6. What is a webservice?? <ul><li>R emote P rocedure C alling protocol that works over HTTP . </li></ul>
  7. 7. Where to use Webservices? <ul><li>Retrieve information dynamically over web </li></ul><ul><ul><li>Service integrations </li></ul></ul><ul><ul><li>Price comparisons </li></ul></ul><ul><ul><li>Hotel bookings </li></ul></ul><ul><li>Web applications requiring integration with diverse programming languages </li></ul>
  8. 8. Why use PHP <ul><li>Already a very popular for web development </li></ul><ul><li>XML support </li></ul><ul><li>CURL support </li></ul><ul><li>OOP </li></ul><ul><li>Potential SOAP extension </li></ul>
  9. 9. SOAP <ul><li>S imple O bject A ccess P rotocol </li></ul><ul><li>HTTP + XML = SOAP </li></ul>
  10. 10. SOAP Message <ul><li>S imple O bject A ccess P rotocol </li></ul><ul><li>HTTP + XML = SOAP </li></ul>
  11. 11. SOAP Request <ul><li>POST /examples HTTP/1.1 </li></ul><ul><li>User-Agent: Radio UserLand/7.0 (WinNT) ‏ </li></ul><ul><li>Host: localhost:81 </li></ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul><ul><li>Content-length: 474 </li></ul><ul><li>SOAPAction: &quot;/examples&quot; </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><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;> </li></ul><ul><li><SOAP-ENV:Body> </li></ul><ul><li><m:getCityName xmlns:m=&quot;http://www.soapware.org/&quot;> </li></ul><ul><li><statenum xsi:type=&quot;xsd:int&quot;>691003</statenum> </li></ul><ul><li></ m:getCityName > </li></ul><ul><li></SOAP-ENV:Body> </li></ul><ul><li></SOAP-ENV:Envelope> </li></ul>Soap envelope Soap body Http Request header
  12. 12. SOAP Response <ul><li>HTTP/1.1 200 OK </li></ul><ul><li>Connection: close </li></ul><ul><li>Content-Length: 499 </li></ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul><ul><li>Date: Wed, 28 Mar 2001 05:05:04 GMT </li></ul><ul><li>Server: UserLand Frontier/7.0-WinNT </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><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;> </li></ul><ul><li><SOAP-ENV:Body> </li></ul><ul><li>< m:getCityNameResponse xmlns:m=&quot;http://www.soapware.org/&quot;> </li></ul><ul><li><Result xsi:type=&quot;xsd:string&quot;>Kollam</Result> </li></ul><ul><li></ m:getCityNameResponse> </li></ul><ul><li></SOAP-ENV:Body> </li></ul><ul><li></SOAP-ENV:Envelope> </li></ul>Soap envelope Soap body Http Response header
  13. 13. SOAP Fault <ul><li>HTTP/1.1 500 Server Error </li></ul><ul><li>Connection: close </li></ul><ul><li>Content-Length: 511 </li></ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul><ul><li>Date: Wed, 28 Mar 2001 05:06:32 GMT </li></ul><ul><li>Server: UserLand Frontier/7.0-WinNT </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><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;> </li></ul><ul><li><SOAP-ENV:Body> </li></ul><ul><li><SOAP-ENV:Fault> </li></ul><ul><li><faultcode>SOAP-ENV:Client</faultcode> </li></ul><ul><li><faultstring>Can't call getCityName because there are too many parameters.</faultstring> </li></ul><ul><li></SOAP-ENV:Fault> </li></ul><ul><li></SOAP-ENV:Body> </li></ul><ul><li></SOAP-ENV:Envelope> </li></ul>Http Response header
  14. 14. What we need in short <ul><li>XML output is not always simplest to put in PHP code </li></ul><ul><li>The XML output is repetitive </li></ul><ul><li>Most of us are lazy </li></ul><ul><li>Less work is better </li></ul><ul><li>In-Short what is needed is a class which abstracts the SOAP messages for us </li></ul>
  15. 15. NuSOAP Toolkit <ul><li>Several PHP toolkits available for SOAP </li></ul><ul><li>NuSOAP usage is simple and efficient </li></ul><ul><li>Object Oriented ... </li></ul><ul><li>URL - http://dietrich.ganx4.com/nusoap/ </li></ul><ul><li>Author - Dietrich Ayala </li></ul><ul><li>Has support for WSDL generation as well </li></ul>
  16. 16. A PHP Function <ul><li>// Return the STD code for the City. </li></ul><ul><li>function getCityName ($pincode){ </li></ul><ul><ul><li>$cityNames = array(‘691003’ => ‘Kollam’, ‘691235’ => ‘Ernakulam’, ‘6945678’ => ‘Trivandrum’); </li></ul></ul><ul><ul><li>return $cityNames[‘$pincode’]; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  17. 17. SOAP Server <ul><li>require_once('nusoap.php'); </li></ul><ul><li>$server = new soap_server; </li></ul><ul><li>$server->register( getCityName '); </li></ul><ul><li>$server->service ($_SERVER['HTTP_RAW_POST_DATA']); </li></ul><ul><li>exit(); </li></ul>
  18. 18. SOAP Client <ul><li>require_once('nusoap.php'); </li></ul><ul><li>$param = array(‘pincode'=>’691003’); </li></ul><ul><li>$client = new soapclient ('http://localhost/service.php?wsdl'); </li></ul><ul><li>$response = $client->call(‘getCityName', $param); </li></ul><ul><li>$response will now have the City name for the pincode passed as parameter... ... </li></ul>
  19. 19. What is missing? <ul><li>What if you don't pass a Pincode ? </li></ul><ul><li>What if you don't get a result? </li></ul><ul><li>What if.... </li></ul><ul><li>SOAP Fault generation </li></ul>Loading...
  20. 20. PHP Function revisted <ul><li>/ Return the STD code for the City. </li></ul><ul><li>function getCityName ($pincode){ </li></ul><ul><ul><li>global $db </li></ul></ul><ul><ul><li>if ($pincode == '') { </li></ul></ul><ul><ul><li>/* Return a SOAP fault indicating a blank pincode */ </li></ul></ul><ul><ul><li>return new soap_fault( </li></ul></ul><ul><ul><li>'Client', '', </li></ul></ul><ul><ul><li>'Must supply a pincode','' </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>$cityNames = array(‘691003’ => ‘Kollam’, ‘691235’ => ‘Ernakulam’, ‘6945678’ => ‘Trivandrum’); </li></ul></ul><ul><ul><li>return $cityNames[‘$pincode’]; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  21. 21. SOAP Client revisted <ul><li>require_once('nusoap.php'); </li></ul><ul><li>$param = array(‘pincode'=>’691003’); </li></ul><ul><li>$client = $client = new soapclient ('http://localhost/service.php?wsdl'); </li></ul><ul><li>$response = $client->call('getCityName', $param); </li></ul><ul><li>if($client->fault){ echo &quot;FAULT: <p>Code: {$client->faultcode} <br />&quot;; echo &quot;String: {$client->faultstring} </p>&quot;; </li></ul><ul><li>} else{ </li></ul><ul><li>echo $response; } </li></ul>
  22. 22. Some considerations <ul><li>SOAP transactions/session </li></ul><ul><li>SOAP authentication and security </li></ul>
  23. 23. SOAP Resources <ul><li>Other PHP SOAP implementations </li></ul><ul><ul><li>PHP-SOAP Extension </li></ul></ul><ul><ul><li>Activestate SWSAPI for PHP </li></ul></ul><ul><ul><li>Manuel Lemos SOAP class </li></ul></ul>
  24. 24. SOAP Resources <ul><li>SOAP and web services reference sites: </li></ul><ul><ul><li>http://www.xml.com/pub/a/2001/04/04/webservices/ XML.com: A Web Services Primer </li></ul></ul><ul><ul><li>http://www.w3c.org/tr/soap - SOAP 1.1 specification </li></ul></ul><ul><ul><li>http://www-106.ibm.com/developerworks/webservices/ - IBM developerWorks Web Services Zone </li></ul></ul>
  25. 25. <ul><li>Thank you. </li></ul>

×