designing & implementing restful web services - ben ramsey
TRANSCRIPT
![Page 1: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/1.jpg)
Designing & ImplementingRESTful Web Services
Ben RamseyInternational PHP Conference
7 November 2006
![Page 2: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/2.jpg)
Welcome
• BenRamsey.com
• I work for Art & Logic, Inc.
• PHP 5 CertificationStudy Guide co-author
• Organizer of AtlantaPHP user group
2
![Page 3: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/3.jpg)
Overview
• Web Services
• REST Overview
• Methods of Data Transport
• Example RESTful Web Services
• Creating RESTful Web Services
3
![Page 4: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/4.jpg)
Web Services
4
![Page 5: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/5.jpg)
What is a Web Service?
• Public interface (API)
• Provides access to data and/or procedures
• On a remote/external system (usually)
• Often uses XML for data exchange
5
![Page 6: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/6.jpg)
Types of Web Services
• XML-RPC
• SOAP
• REST
6
![Page 7: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/7.jpg)
REST Overview
7
![Page 8: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/8.jpg)
What is REST?
8
• Representational State Transfer
• Term originated in 2000 in Roy Felding’s doctoral dissertation about the Web entitled “Architectural Styles and the Design of Network-based Software Architectures”
![Page 9: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/9.jpg)
Theory of REST
• Focus on diversity of resources (nouns), not actions (verbs)
• Every resource is uniquely addressable
• All resources share the same constrained interface for transfer of state (actions)
• Must be stateless, cacheable, and layered
9
![Page 10: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/10.jpg)
What Does It Mean?
“[REST] is intended to evoke an image of how a well-designed Web application behaves: a network of web pages (a virtual state-machine), where the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.” — Roy Felding
10
![Page 11: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/11.jpg)
Web As Prime Example
• URIs uniquely address resources
• HTTP methods (GET, POST, HEAD, etc.) and content types provide a constrained interface
• All transactions are atomic
• HTTP provides cache control
11
![Page 12: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/12.jpg)
Well-RESTed
• Applications adhering to REST principles are said to be RESTful
• Extreme advocates of REST are often called RESTafarians
12
![Page 13: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/13.jpg)
Relaxing REST
• Any simple interface using XML over HTTP (in response to GET requests)
• That is also not RPC-based
• May use JSON, YAML, plain text, etc. instead of XML
• In most PHP applications, this is what we mean when we say “REST”
13
![Page 14: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/14.jpg)
Methods of Data Transport
14
![Page 15: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/15.jpg)
XML Over HTTP
15
• It’s an extensible mark-up language
• This makes it very flexible
• Lightweight and easy to parse
• Ease of communication between disparate systems
![Page 16: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/16.jpg)
Parsing XML With PHP
• SimpleXML or DOM
16
![Page 17: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/17.jpg)
JSON Over HTTP
17
• JavaScript Object Notation
• Makes it easy to pass arrays and objects from PHP to JavaScript and vice versa
• Very useful and efficient in Ajax applications
• More lightweight than XML and easy to parse
![Page 18: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/18.jpg)
Parsing JSON With PHP
• ext/json and Zend_JSON
18
![Page 19: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/19.jpg)
Which Method Is the Best?
• JSON is very lightweight but intended for JavaScript; useful for passing data to/from a front-end
• XML is very flexible and better for many other destinations (front-end, rich clients, other servers, etc.)
• The tools are available; the choice is yours
19
![Page 20: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/20.jpg)
Example RESTful Web Services
20
![Page 21: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/21.jpg)
del.icio.us
21
• Public and authenticated REST access
• All requests over SSL using HTTP-Auth
• Requests a 1-second delay between queries
• Very simple API
• http://del.icio.us/help/api/
![Page 22: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/22.jpg)
22
delicious.php
![Page 23: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/23.jpg)
Yahoo!
• Web Search Service is RESTful
• Requires an application ID, but no special authentication or handshake
• Limit 5,000 queries per IP address per day
• http://developer.yahoo.com/search/web/V1/webSearch.html
23
![Page 24: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/24.jpg)
24
yahoo.php
![Page 25: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/25.jpg)
Flickr
• Provides a variety of Web Service interfaces, including REST
• Accomplished in an RPC fashion
• Uses a complex token authentication handshake to access user data
• http://flickr.com/services/api/
25
![Page 26: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/26.jpg)
26
login.php
![Page 27: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/27.jpg)
27
flickr.php
![Page 28: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/28.jpg)
28
flickr.php
![Page 29: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/29.jpg)
29
flickr.php
![Page 30: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/30.jpg)
30
flickr.php
![Page 31: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/31.jpg)
Creating RESTful Web Services
31
![Page 32: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/32.jpg)
Why Provide a Service?
32
• You have a service that benefits your users best if they can get to their data from outside the application
• You want others to use your data store in their applications
• All the cool kids are doing it
![Page 33: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/33.jpg)
Designing a RESTful Service
33
• Adhere to the principles of REST
• Diverse resources/nouns
• Unique address for each resource
• Constrained interface for resources (GET)
• Transfers are atomic/stateless
• Your URI structure is your API
![Page 34: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/34.jpg)
Designing a RESTful Service
• Example: Catalog of books
• Design the application with a specific URI structure in mind
• http://example.org/catalog
• http://example.org/catalog/book
• http://example.org/catalog/book/1234
34
![Page 35: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/35.jpg)
Designing a RESTful Service
• We can expand our catalog and service with ease
• http://example.org/catalog/movie
• http://example.org/catalog/movie/1234
• Keep the URIs clean and simple
• URIs should indicate the kind of data the consumer will receive
35
![Page 36: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/36.jpg)
/catalog/book?isbn=014143984X
36
![Page 37: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/37.jpg)
/catalog/book?isbn=014143984X&format=json
37
![Page 38: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/38.jpg)
Implementing a RESTful Service
• Use DOM to generate XML documents from a data model
• Use json_encode() to convert arrays/objects into JSON
• Use Zend_Rest_Server to create a REST server
38
![Page 39: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/39.jpg)
Using Zend_Rest_Server
• Determine the URI structure of the service
• /catalog/book?isbn=123456789X
• Create a CatalogController class with a bookAction() method
• Create a catalog class to return data from the model to the REST server class
39
![Page 40: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/40.jpg)
40
CatalogController.php
![Page 41: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/41.jpg)
41
catalog.php
![Page 42: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/42.jpg)
/catalog/book?isbn=014143984X
42
![Page 43: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/43.jpg)
Zend_Rest_Server Caveats
• Zend Framework is at Preview 0.2.0
• Zend_Rest_Server is in the “incubator”
• Works only for very simple solutions
• Cannot handle multidimensional arrays yet
• For more than one level of tags, return a SimpleXMLElement object
• Only returns XML
43
![Page 44: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/44.jpg)
Security Concerns
• A Web Service accepts data from remote applications/machines
• Filter all input
• Output as XML, JSON, etc.
• Escape output accordingly
• For authentication and sensitive data, force the use of SSL
44
![Page 45: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/45.jpg)
Summary
• Creating RESTful Web Services
• Example RESTful Web Services
• Methods of Data Transport
• REST Overview
• Web Services
45
![Page 46: Designing & Implementing RESTful Web Services - Ben Ramsey](https://reader036.vdocuments.us/reader036/viewer/2022071600/613d1994736caf36b7594f88/html5/thumbnails/46.jpg)
Slides & Further Reading
http://benramsey.com/archives/ipc06-slides/
And on the Conference CD-ROM
46