What is a Web Service?
A “web service” is a network accessible interface to application functionality built using XML and usually HTTP.
Standard Setup
* • Clients make requests and servers reply
* • Communication done over the Internet
Four Basic Steps
* 1. Web Services client script builds up the request
* 2. Client sends the request to server using HTTP
* 3. Server replies and returns an XML document with results
* 4. Client parses XML
Three forms of Web Services
* • SOAP
* • XML-RPC
* • REST
SOAP
* • Formerly known as Simple Object Access Protocol.
* • Uses XML, but you never need to touch it.
* • Which is good, because SOAP is messy to look at.
* • You just call functions and manipulate arrays. (Unless you want to.)
* • A few PHP implementations: PEAR::SOAP, PHP-SOAP, NuSOAP
Make a Request
Load in the SOAP Client
<?php require ‘SOAP/Client.php’; ?>
Generate Client Proxy (for Amazon.Com)
<?php
// We have human readable explanation of the API.
$wsdl_url = ‘http://soap.amazon.com/schemas3/AmazonWebServices.wsdl’;
$WSDL = new SOAP_WSDL($wsdl_url);
$client = $WSDL->getProxy();
?>
Web Service Description Language (WSDL)
Machine readable description (XML) of a web service. Used here to define server’s methods and parameters.
SOAP Server
<?php
require_once ‘SOAP/Server.php’;
class SOAP_Server_rot13 {
function rotate($input) {
return str_rot13($input);
}
}
$server = new SOAP_Server;
$soapclass = new SOAP_Server_rot13();
// Associate PHP class with SOAP message
$server->addObjectMap($soapclass ,’urn:SOAP_Server_rot13′);
$server->service($HTTP_RAW_POST_DATA);
?>
XML-RPC
* • XML Remote Procedure Call
* • Similar to SOAP, but less complex
* • Which is its biggest advantage
* • And its biggest disadvantage
* • But it is often “good enough”
* • But, SOAP has better buzzword compliance
REST
* • REpresentational State Transfer
* • Make URI request using existing HTTP methods: GET / POST / PUT / DELETE.
* • Data returned as XML, and you do need to touch it.
* • Which is good, because it’s not complicated.
* • Many ways to parse XML: SAX / DOM / XSLT / SimpleXML