Professional Documents
Culture Documents
About Me
http://lornajane.net @lornajane
PHP
PHP: "solving the web problem" The problem evolves PHP evolves
PHP
Server Client Web application tool Command line tool GTK ...
PHP
Web Services
Architecture
Traditional Architecture
Traditional Architecture
Additional Channels
Services Architecture
Data Formats
JSON
JavaScript Object Notation Natively read/write in most languages Very simple! (we like simple) Limitations
XML
eXtensible Markup Language Familiar Can give more detail than JSON Native read/write in most languages
SimpleXML Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php $xml = <<< XML <?xml version="1.0" ?> <menus> <menu>Lunch</menu> <menu>Dinner</menu> <menu>Dessert</menu> <menu>Drinks</menu> </menus> XML; $simplexml = new SimpleXMLElement($xml); var_dump($simplexml);
SimpleXML Example
object(SimpleXMLElement)#1 (1) { ["menu"]=> array(5) { [0]=> string(5) "Lunch" [1]=> string(6) "Dinner" [2]=> string(7) "Dessert" [3]=> string(6) "Drinks" } }
SimpleXML Example
1 <?php 2 3 $simplexml = simplexml_load_string('<?xml version="1.0" ?><menus/>'); 4 $simplexml->addChild('menu','Lunch'); 5 $simplexml->addChild('menu','Dinner'); 6 $simplexml->addChild('menu','Drinks'); 7 $simplexml->addChild('menu','Dessert'); 8 9 echo $simplexml->asXML();
SimpleXML Example
<?xml version="1.0"?> <menus> <menu>Lunch</menu> <menu>Dinner</menu> <menu>Dessert</menu> <menu>Drinks</menu> </menus>
Service Types
Service Types
*-RPC
XML-RPC JSON-RPC
SOAP REST
RPC
All URLs point to a single endpoint Parameters give method names Request body can take a variety of formats
Using Flickr's XML-RPC Test method: just echoes back to user XML formatted data
Flickr Response
<?xml version="1.0" encoding="utf-8" ?> <methodResponse> <params> <param> <value> <string><api_key>54rt346</api_key> </string> </value> </param> </params> </methodResponse>
RPC Advantages
RPC is a great format for wrapping existing functionality Can abstract between existing systems Familiar functional paradigm
Delivering RPC
Wrapping RPC
RPC is a library-like interface Can easily wrap existing libraries to call like this Can wrap an interface to an RPC service to look like a library
SOAP
Special case of RPC using XML Has given formats for messages and errors Libraries exist for creating server and client in most languages
WSDL
Web Service Description Language Widely used with SOAP Describes the methods, arguments and data types available IDEs can read this and hint Validity of requests is checked before they are sent
WSDL
<?xml version ='1.0' encoding ='UTF-8' ?> <definitions name='MyClass' http://schemas.xmlsoap.org/soap/encoding/' targetNamespace='urn:MyClassInventory' <message name='getAccountStatusRequest'> </message> <part name='accountID' type='xsd:string'/> xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' xmlns:tns='urn:MyClassInventory' xmlns='http://schemas.xmlsoap.org/wsdl/'> xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenc=' <message name='getAccountStatusResponse'> <part name='accountID' type='xsd:string'/> <part name='counter' type='xsd:float' />
</message>
<portType name='MyClassPortType'>
<operation name='getAccountStatus'>
<input message='tns:getAccountStatusRequest'/>
</operation> </portType>
<output message='tns:getAccountStatusResponse'/>
<binding name='MyClassBinding' type='tns:MyClassPortType'> <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> <soap:operation soapAction='urn:xmethods-delayed-quotes#getAccountStatus'/> <input> <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
<operation name='getAccountStatus'>
</input>
<output>
</binding>
</operation>
</output>
<service name='MyClassService'>
<soap:address location='http://rivendell.local:10002/MyClassServiceServer.php'/>
</service>
</definitions>
Delivering SOAP
In WSDL mode, only the WSDL needs to be supplied Otherwise method names, arguments and types will be needed
REST
A series of concepts Generally uses HTTP (HyperText Transfer Protocol) URLs are resource locations Verbs tell the service what to do Status codes indicate what the outcome was
Implementing REST
Standard application architecture Routing to map requests to internal functionality Output not always HTML
REST CRUD
Action Retrieve Create Update Delete HTTP Verb GET POST PUT DELETE
REST Examples
GET
http://localhost/users http://localhost/users/harry
POST
http://localhost/users
PUT
http://localhost/users/harry
Health Warning!
Delivering REST
Full documentation including URL formats, data types, and response formats Must include information about error handling
REST as an inspiration
RESTful is a strict definition REST is full of great ideas REST is great for clean, simple, robust services Cherry-pick the bits that work
Resources
RESTful Web Services Leonard Richardson and Sam Ruby http://php.net http://benramsey.com http://lornajane.net
Questions ???
Thankyou
http://slideshare.net/lornajane