Implementing webservices with PHP - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Implementing webservices with PHP

Description:

Who this talk is for? PHP developers with moderate degree of PHP expertise ... User-Agent: Radio UserLand/7.0 (WinNT) Host: localhost:81. Content-Type: text/xml; ... – PowerPoint PPT presentation

Number of Views:151
Avg rating:3.0/5.0
Slides: 25
Provided by: foss
Category:

less

Transcript and Presenter's Notes

Title: Implementing webservices with PHP


1
Implementing webservices with PHP
Dr. Tarique Sani CTO, SANIsoft Nagpur, India
2
Who this talk is for?
  • PHP developers with moderate degree of PHP
    expertise
  • PHP developers wanting to implement SOAP based
    webservice
  • PHP developers who think webservices is rocket
    science
  • It is not intended for ASP.NET developers

3
What will be covered
  • What are web services
  • Basics of SOAP
  • Implementing a simple webservice using NuSOAP

4
What is a webservice?
  • Loosely coupled, reusable software components
    that semantically encapsulate discrete
    functionality and are distributed and
    programmatically accessible over standard
    Internet protocols

5
What is a webservice??
  • Remote Procedure Calling protocol that works
    over HTTP.

6
Where to use Webservices?
  • Retrieve information dynamically over web
  • Stock Quotes
  • Price comparisions
  • Hotel bookings
  • Web applications requiring integration with
    diverse programming languages

7
Why use PHP
  • Already a very popular for web development
  • XML support
  • CURL support
  • OOP
  • Potential SOAP extension

8
SOAP
  • Simple Object Access Protocol
  • HTTP XML SOAP

9
SOAP Message
  • Simple Object Access Protocol
  • HTTP XML SOAP

10
SOAP Request
  • POST /examples HTTP/1.1
  • User-Agent Radio UserLand/7.0 (WinNT)
  • Host localhost81
  • Content-Type text/xml charsetutf-8
  • Content-length 474
  • SOAPAction "/examples"
  • lt?xml version"1.0"?gt
  • ltSOAP-ENVEnvelope SOAP-ENVencodingStyle"http//
    schemas.xmlsoap.org/soap/encoding/"
    xmlnsSOAP-ENC"http//schemas.xmlsoap.org/soap/en
    coding/" xmlnsSOAP-ENV"http//schemas.xmlsoap.or
    g/soap/envelope/" xmlnsxsd"http//www.w3.org/199
    9/XMLSchema" xmlnsxsi"http//www.w3.org/1999/XML
    Schema-instance"gt
  • ltSOAP-ENVBodygt
  • ltmgetStateName xmlnsm"http//www.soapware
    .org/"gt
  • ltstatenum xsitype"xsdint"gt41lt/statenum
    gt
  • lt/mgetStateNamegt
  • lt/SOAP-ENVBodygt
  • lt/SOAP-ENVEnvelopegt

11
SOAP Response
  • HTTP/1.1 200 OK
  • Connection close
  • Content-Length 499
  • Content-Type text/xml charsetutf-8
  • Date Wed, 28 Mar 2001 050504 GMT
  • Server UserLand Frontier/7.0-WinNT
  • lt?xml version"1.0"?gt
  • ltSOAP-ENVEnvelope SOAP-ENVencodingStyle"http//
    schemas.xmlsoap.org/soap/encoding/"
    xmlnsSOAP-ENC"http//schemas.xmlsoap.org/soap/en
    coding/" xmlnsSOAP-ENV"http//schemas.xmlsoap.or
    g/soap/envelope/" xmlnsxsd"http//www.w3.org/199
    9/XMLSchema" xmlnsxsi"http//www.w3.org/1999/XML
    Schema-instance"gt
  • ltSOAP-ENVBodygt
  • ltmgetStateNameResponse xmlnsm"http//www.
    soapware.org/"gt
  • ltResult xsitype"xsdstring"gtSouth
    Dakotalt/Resultgt
  • lt/mgetStateNameResponsegt
  • lt/SOAP-ENVBodygt
  • lt/SOAP-ENVEnvelopegt

12
SOAP Fault
  • HTTP/1.1 500 Server Error
  • Connection close
  • Content-Length 511
  • Content-Type text/xml charsetutf-8
  • Date Wed, 28 Mar 2001 050632 GMT
  • Server UserLand Frontier/7.0-WinNT
  • lt?xml version"1.0"?gt
  • ltSOAP-ENVEnvelope SOAP-ENVencodingStyle"http//
    schemas.xmlsoap.org/soap/encoding/"
    xmlnsSOAP-ENV"http//schemas.xmlsoap.org/soap/en
    velope/" xmlnsxsd"http//www.w3.org/1999/XMLSche
    ma" xmlnsxsi"http//www.w3.org/1999/XMLSchema-in
    stance"gt
  • ltSOAP-ENVBodygt
  • ltSOAP-ENVFaultgt
  • ltfaultcodegtSOAP-ENVClientlt/faultcodegt
  • ltfaultstringgtCan't call getStateName
    because there are too many parameters.lt/faultstrin
    ggt
  • lt/SOAP-ENVFaultgt
  • lt/SOAP-ENVBodygt
  • lt/SOAP-ENVEnvelopegt

13
WAKE UP!!!!The next slide has pics of naked
ladies I took in Thailand
14
Sorry!! but...
  • 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
  • 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

16
A PHP Function
  • // Return the STD code for the City.
  • function getSTD (city)
  • global db
  • query"SELECT Code FROM cities WHERE
    cityNamecity"
  • db-gtquery(query)
  • return db-gtf("Code")

17
SOAP Server
  • require_once('nusoap.php')
  • server new soap_server
  • server-gtregister('getSTD')
  • server-gtservice (_SERVER'HTTP_RAW_POST_DATA')
  • exit()

18
SOAP Client
  • require_once('nusoap.php')
  • param array('city'gt_GET'city')
  • client new soapclient ('http//sanisoft.com/
    getSTD.php')
  • response client-gtcall('getSTD', param)
  • response will now have the STD code for the city
    passed as parameter... ...

19
What is missing?
  • What if you don't pass a City name?
  • What if you don't get a result?
  • What if....
  • SOAP Fault generation

20
PHP Function revisted
  • / Return the STD code for the City.
  • function getSTD (city)
  • global db
  • if (city '')
  • / Return a SOAP fault indicating a blank
    city name /
  • return new soap_fault(
  • 'Client', '',
  • 'Must supply a city name',''
  • )
  • query"SELECT Code FROM cities WHERE
    cityNamecity"
  • db-gtquery(query)
  • return db-gtf("Code")

21
SOAP Client revisted
  • require_once('nusoap.php')
  • param array('city'gt_GET'city')
  • client new soapclient ('http//sanisoft.com/
    getSTD.php')
  • response client-gtcall('getSTD', param)
  • if(client-gtfault)echo "FAULT ltpgtCode
    client-gtfaultcode ltbr /gt" echo "String
    client-gtfaultstring lt/pgt"
  • else
  • echo response

22
Some considerations
  • SOAP vs XML-RPC
  • SOAP transactions/session
  • SOAP authentication and security

23
SOAP Resources
  • Other PHP SOAP implementations
  • PHP-SOAP Extension
  • Activestate SWSAPI for PHP
  • PEAR
  • Krysalis
  • Manuel Lemos SOAP class

24
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
Write a Comment
User Comments (0)
About PowerShow.com