2. About Me
●
Lorna Jane Mitchell
●
PHP Developer/Trainer/Consultant at Ibuildings
●
Hosting DPC
●
Editor at techPortal
●
http://lornajane.net
●
@lornajane
3. PHP
●
PHP: "solving the web problem"
●
The problem evolves
●
PHP evolves
4. PHP
●
Server
●
Client
●
Web application tool
●
Command line tool
●
GTK ...
5. PHP
●
Ubiquitous
●
Runs on your platform
●
*nix
●
mac
●
windows (yes really, rather well!)
●
Can talk to your other systems
6. Web Services
●
"scalable, reusable libraries"
●
Interface over HTTP
●
Modular application design
14. JSON
●
JavaScript Object Notation
●
Natively read/write in most languages
●
Very simple! (we like simple)
●
Limitations
●
no data typing
●
no distinction between object and array
32. RPC Advantages
●
RPC is a great format for wrapping existing
functionality
●
Can abstract between existing systems
●
Familiar functional paradigm
33. Delivering RPC
●
Consumers will need
– Service URL
– Docs of functions and arguments
– If this was an existing system, existing docs may
suffice
34. 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
36. SOAP
●
Special case of RPC using XML
●
Has given formats for messages and errors
●
Libraries exist for creating server and client in
most languages
37. PHP SOAP Server Example
require_once('lib/myClass.php');
$server = new SoapServer("service.wsdl");
$server->setClass("MyClass");
$server->handle();
38. PHP SOAP Client Example
$wsdl = "Service.wsdl";
$client = new SoapClient($wsdl, $params);
$output = $client->requestShot(
'http://www.php.net','', 300, 400);
39. 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
41. Delivering SOAP
●
In WSDL mode, only the WSDL needs to be
supplied
●
Otherwise method names, arguments and
types will be needed
42. 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
43. Implementing REST
●
Standard application architecture
●
Routing to map requests to internal functionality
●
Output not always HTML
44. REST CRUD
Action HTTP Verb
Retrieve GET
Create POST
Update PUT
Delete DELETE
45. REST Examples
●
GET
●
http://localhost/users
●
http://localhost/users/harry
●
POST
●
http://localhost/users
●
PUT
●
http://localhost/users/harry
46. REST from PHP: GET
1 <?php
2
3 $result = file_get_contents('http://localhost/users');
4 var_dump($result);
47. REST from PHP: GET
1 <?php
2
3 $ch = curl_init('http://localhost/users');
4
5 curl_exec($ch);
●
Health Warning!
●
curl will echo output
●
use CURLOPT_RETURNTRANSFER to capture it
instead
51. Delivering REST
●
Full documentation including URL formats, data
types, and response formats
●
Must include information about error handling
52. 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
●
Just don't call it "RESTful" :)
53. Resources
●
RESTful Web Services – Leonard Richardson
and Sam Ruby
●
http://php.net
●
http://benramsey.com
●
http://lornajane.net