Building web services with Zend Framework
King FooJonas Mariën
13 July 2010, Vlissingen NL
This talk = betaremarks and suggestions are welcome
King Foo
Launched January 2010
5 experienced PHP developers (4 Zend Certified Engineers, MySQL, Linux, scaling, and architecture experience
LAMP, but wandering off into different grounds at regular intervals
Having fun with mobile stuff and web services for example ...
This talk
Not about talking to Flickr or Google
About others talking to us
So we're going to create web services:
Using Zend Framework
For others to consume
Question time
Heard about / tried Zend Framework?
Zend Framework
Components, high quality
Sufficient documentation
MVC if you want
Nice server components
Zend Framework
Download, unpackwget http://framework.zend.com/releases/ZendFramework-1.10.6/ZendFramework-1.10.6.tar.gz
tar zxvf ZendFramework-1.10.6.tar.gz
cp -ar ZendFramework-1.10.6/library .
cp -ar ZendFramework-1.10.6/bin .
alias zf=bin/zf.sh
Create project, module, create controller with views
zf create project .
zf create module admin
zf create controller index index-action-included=1 admin
Zend Framework
You need one more thing to enable modules:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
Plus, we want our own libraries, eventually:autoloaderNamespaces.kingfoo = "Kingfoo_"
Zend Framework
.
|-- application
| |-- Bootstrap.php
| |-- configs
| | `--
.
`-- admin
|-- controllers
| `-- IndexController.php
|-- models
`-- views
|-- filters
|-- helpers
`-- scripts
.
|-- application
| |-- Bootstrap.php
| |-- configs
| | `--
Zend Framework
Create Apache vhost
Add to /etc/hosts
Reload Apache
Question time
Heard about / tried web services?
Web services
From Wikipedia:Web services are typically application programming interfaces (API) or web APIs that are accessed via Hypertext Transfer Protocol (HTTP) and executed on a remote system hosting the requested services. Web services tend to fall into one of two camps: Big Web Services and RESTful Web Services.
Different kinds of WS
SOAP
XML-RPC
JSON-RPC
REST
BTW, I don't want to talk about Zend_Amf
Exposing something
class Kingfoo_Quote {
public function __construct() {
$array[0] = 'Chuck Norris counted to infinity - twice.';
$array[9] = 'Chuck Norris doesn’t wash his clothes, he disembowels them.';
$this->_quotes = $array;
}
SOAP
Simple Object Access Protocol (now just SOAP)
Big spec, even bigger with WS-*
RPC style, method oriented
Server and WSDL
SOAP 1.1 and SOAP 1.2
SOAP 1.2 allows non-HTTP too
SOAP governed by a W3C working group
SOAP with ZF
SOAP with plain PHP works, but can be cumbersome
The real power: WSDL generation and reflection being used for discovery
Strategies for discovering more complex messages and responses are available
Docblock comments are very important here
SOAP with ZF
if (isset($_GET['WSDL'])) {
$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('Kingfoo_Quote');
$autodiscover->setUri('http://presentation-zfws/SOAP.php');
$autodiscover->handle();
SOAP with ZF
Request
Response
soapUI
XML-RPC
Exactly: XML over RPC
Predecessor of SOAP
Limited set of datatypes, nothing like WSDL
Spec:
The request body is wrapped in a single methodCall tag
That container should contain a single methodName tag
The method name should be a string only containin a-z,A-Z,0-9 and any of these
XML-RPC
REQUEST:<?xml version="1.0"?><methodCall> <methodName>zfwsdemo.getQuote</methodName> <params> <param> <value><int>2</int></value> </param> </params></methodCall></code>
RESPONSE<code><?xml version="1.0"?><methodResponse> <params> <param> <value>... Chuck Norris ...</value> </param> </params></methodResponse></code>
XML-RPC
More complex values:
Array
Struct
Error messages
system.* (listMethods, methodSignature, methodHelp, multicall ...)
XML-RPC with ZF
SERVER
$server = new Zend_XmlRpc_Server();
$server->setClass('Kingfoo_Quote','zfwsdemo');
echo $server->handle();
CLIENT
JSON-RPC
JSON message format
TCP/IP sockets are allowed too (not only HTTP)
Request, response, error object, batches
Client and server are peers, sending each other notifications
Two versions: 1.0 and 2.0(ZF supports both)
Support for batches
JSON-RPC with ZF
SERVER
$server = new Zend_Json_Server();
$server->setClass('Kingfoo_Quote');
$server->handle();
CLIENT
$request = new stdClass();
$request->jsonrpc = '2.0';
JSON-RPC with ZF
AND NOW WITH SMD
$server = new Zend_Json_Server();
$server->setClass('Kingfoo_Quote');
if ('GET' == $_SERVER['REQUEST_METHOD']) {
$server->setTarget('/JSONRPC.php')
->setEnvelope(Zend_Json_Server_Smd::E
JSON-RPC with ZF
JSON-RPC very interesting with for example the jQery Zend JSON-RPC plugin
(http://plugins.jquery.com/project/zendjsonrpc) proxy = jQuery.Zend.jsonrpc({url: '/JSONRPC.php'});
result = proxy.getList();
A demo ...
Possible with Dojo too, see ZF manual
REST
Representational State Transfer (also ReST)
Resource oriented
XML, increasingly JSON as message format
HTTP verbs define nature of action on resource
No spec or standard, more like a 'movement'
RESTafarians leave funny comments on blogs and have strong opinions. True
REST
GET POST PUT DELETE
/quotes Get list Add quote to list
Update entire list
Delete entire list
/quotes/<id> Get quote with id <id>
Add subquote (unlikely)
Update single quote
Delete single quote
List = oftern referred to as a 'collection'
REST with ZF
At first conceived as a standalone service, much like Zend_Soap: Zend_Rest
Now more and more integrated, using Zend_Rest_Controller and Zend_Rest_Route
Future versions of ZF (2.0?) will probably have it fully integrated in the controller, using context switching
(see posts by Matthew Weier O'Phinney at http://weierophinney.net/matthew/)
REST with ZF
Zend_Rest_Controller
controller implementing it must implement these actions:
class Rest_IndexController extends Zend_Rest_Controller {
public function init() {}
REST with ZF
Zend_Rest_Route
Add module
zf create module rest
Add to Bootstrap
protected function _initRestroutes() {
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
REST with ZF
Zend_Rest_Route detects HTTP verbs like POST and points them to a predefined list of
corresponding URI end points. Our example code will result in behaviour like
this:
URI Module_Controller action
GET /rest/quote/ Rest_QuoteController indexAction()
REST with ZF
SERVER
-> code example: route + controller
CLIENT
Zend_Rest_Client -> no
$client = new Zend_Http_Client('http://presentation-zfws/rest/index/2');
REST – Response codes
Error and response codes ! For example:
200 OK Everything allright
201 Created The object or resources is created
204 Empty body Resource is deleted
400 Bad Request Request is
REWIND
We had an overview of different kinds of webservices that you can create using ZF
RPC vs Resource oriented solutions
What do you choose? Depends on your requirements
Extras
How can we restrict access to our service?
What about performance?
Automated documentation for your peers
Extending the existing ZF classes and making things even more easy for yourself
Thanks for your time
Questions?
Want us to work for you?
www.king-foo.be
@jonasmarien
Top Related