Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

37
Building web services with Zend Framework King Foo Jonas Mariën 13 July 2010, Vlissingen NL

Transcript of Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Page 1: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Building web services with Zend Framework

King FooJonas Mariën

13 July 2010, Vlissingen NL

Page 2: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

This talk = betaremarks and suggestions are welcome

Page 3: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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 ...

Page 4: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 5: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Question time

Heard about / tried Zend Framework?

Page 6: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Zend Framework

Components, high quality

Sufficient documentation

MVC if you want

Nice server components

Page 7: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 8: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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_"

Page 9: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Zend Framework

.

|-- application

| |-- Bootstrap.php

| |-- configs

| | `--

.

`-- admin

|-- controllers

| `-- IndexController.php

|-- models

`-- views

|-- filters

|-- helpers

`-- scripts

.

|-- application

| |-- Bootstrap.php

| |-- configs

| | `--

Page 10: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Zend Framework

Create Apache vhost

Add to /etc/hosts

Reload Apache

Page 11: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Question time

Heard about / tried web services?

Page 12: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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.

Page 13: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Different kinds of WS

SOAP

XML-RPC

JSON-RPC

REST

BTW, I don't want to talk about Zend_Amf

Page 14: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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;

}

Page 15: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 16: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 17: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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();

Page 18: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

SOAP with ZF

Request

Response

soapUI

Page 19: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 20: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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>

Page 21: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

XML-RPC

More complex values:

Array

Struct

Error messages

system.* (listMethods, methodSignature, methodHelp, multicall ...)

Page 22: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

XML-RPC with ZF

SERVER

$server = new Zend_XmlRpc_Server();

$server->setClass('Kingfoo_Quote','zfwsdemo');

echo $server->handle();

CLIENT

Page 23: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 24: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

JSON-RPC with ZF

SERVER

$server = new Zend_Json_Server();

$server->setClass('Kingfoo_Quote');

$server->handle();

CLIENT

$request = new stdClass();

$request->jsonrpc = '2.0';

Page 25: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 26: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 27: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 28: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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'

Page 29: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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/)

Page 30: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

REST with ZF

Zend_Rest_Controller

controller implementing it must implement these actions:

class Rest_IndexController extends Zend_Rest_Controller {

public function init() {}

Page 31: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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();

Page 32: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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()

Page 33: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

REST with ZF

SERVER

-> code example: route + controller

CLIENT

Zend_Rest_Client -> no

$client = new Zend_Http_Client('http://presentation-zfws/rest/index/2');

Page 34: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 35: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 36: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

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

Page 37: Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vlissingen)

Thanks for your time

Questions?

Want us to work for you?

[email protected]

www.king-foo.be

@jonasmarien