rest introduction (php london)
DESCRIPTION
An introduction to Representation State Transfer (REST) and how it's philosophies can alter the way you design and build web applications.TRANSCRIPT
Representational State Transfer
RESTful Principles
A universal syntax for resource-identification
A set of well-defined operations
Having a shared set of media-types
The use of hypermedia for application state-transitions
Statelessprotocol
Building a RESTful App
RESTfully Delicious
● Get a list of all bookmarks● filter by user and/or tag● limit by number
● Add a bookmark● Edit a bookmark● Delete a bookmark
Discover Resources
Resources URLs Methods RepresentationsBookmark /bookmarks/{md5} GET
PUTDELETE
Bookmark list /bookmarks GETPOST
Users bookmarks /users/{user} GETTagged bookmarks /tags/{tag} GET
application/bookmark+xmlapplication/bookmark+xml
application/atom+xmlapplication/bookmark+xmlapplication/atom+xmlapplication/atom+xml
Resources URLs Methods RepresentationsUser list /users GETTag list /tags GETHomepage / GET
application/atom+xmlapplication/atom+xmlapplication/delicious+xml
GETting the Homepage
GET /
200 OK
Contenttype: application/delicious+xml
<?xml version=”1.0”?>
<delicious users=”/users” bookmarks=”/bookmarks” tags=”/tags”>
<recent start=”1” end=”20” next=”/?start=21&end=40”>
<bookmark url="http://www.example.org/somethingintersting" href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>
...
</recent>
</delicious>
GETting Bookmarks
GET /bookmarks
200 OK
Contenttype: application/atom+xml
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Bookmarks</title>
<entry>
<title>Something interesting</title>
<link href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>
<summary>http://example.org/somethingintersting</summary>
</entry>
</feed>
GETting A Bookmark
GET /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
200 OK
Contenttype: application/bookmark+xml
<?xml version="1.0"?>
<bookmark>
<title>Something interesting</title>
<url>http://example.org/somethingintersting</url>
<user href=”/users/pauljames”>pauljames</user>
<tags>
<tag href=”/tags/interesting”>interesting</tag>
</tags>
</bookmark>
Creating and Updating
POST /bookmarks
Contenttype: application/bookmark+xml
...
201 Created
Location: /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
PUT /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
Contenttype: application/bookmark+xml
...
200 OK
Benefits of Being RESTful
● Better scaling due to stateless communications ● Better response times due to caching● Better longterm compatibility due to:
● the capability of document types to evolve without breaking backwardscompatibility
● the ability to add support for new content types without reducing support for older content types.
● Do less with more
Finally, some PHP
HTTP method$_SERVER['REQUEST_METHOD']
Reading request data$_SERVER['CONTENT_LENGTH']
$_SERVER['CONTENT_TYPE']
POST - $HTTP_RAW_POST_DATA
Other HTTP methods - fopen('php://input', 'r')
Processing request dataSimpleXML, MiniXML, PHP JSON, parse_str, unserialize,
unpack, preg_match, etc.
Generating ResponsesHTTP response codes
200 OK, 201 Created, 204 No Content, 304 Not Modified, 401 Unauthorized, 404 Not Found, 405 Method Not Allowed, 411 Length Required, 415 Unsupported Media Type
Generating representationsSmarty, PHPFastTemplate, GD, SimpleXML, printf, etc.
HTTP cachingExpires, Cache-Control, Etag, Last-Modifiedheader('Expires: '.gmdate('D, j M Y H:i:s T', time() + $cachelength));
header('CacheControl: maxage='.$cachelength.', mustrevalidate');
Conclusion
● REST is not just about “Web Services”● Set of best practices for building Web apps
● Give everything a URL● Use correct HTTP methods● Use common media types● Link things together with hypertext
Further Reading
● Roger L. Costello (http://www.xfront.com/RESTWebServices.html)
● Paul Prescod (http://www.prescod.net/rest/)● REST Wiki (http://rest.blueoxen.net/)● Atom Publishing Protocol
(http://atomenabled.org/developers/protocol/)● http://del.icio.us/tag/rest
1
IntroductionQuestions
Overloaded term
POX over HTTP?Other HTTP methods?Tidy URLs?Buzzword?
http://flickr.com/photos/estherase/128983854/
2
Representational State Transfer
Architectural styleDistilled from the Web by Roy FieldingHTTP 1.1 was designed to conform to RESTDefines how the Web worksDescribes a set of rules for building applications on the
Web that exhibit certain desirable propertiesREST is not HTTP, but HTTP is RESTful
“it’s the way the Web already works, just formalized a bit and with some do’s and don’ts.”
Web service is a Web page that’s meant to be consumed by an autonomous program as opposed to a Web browser
http://flickr.com/photos/practicalowl/392894653/
3
RESTful Principles
5 principles
● A universal syntax for resourceidentification URLs● A set of welldefined operations● Having a shared set of mediatypes● The use of hypermedia for application statetransitions● Stateless protocol – all state on the client
http://flickr.com/photos/thowi/113223967/
4
A universal syntax for resource-identification
● Uniform resource locators (URLs)● Every resource (thing of interest) has a URL● URLs are unique and allow us to dereference a
resource● Nouns● Trillions of nouns for all the concepts in all the heads
and files of all the people in the world
http://flickr.com/photos/joeholmes/258136938/
5
A set of well-defined operations
● HTTP methods verbs● GET – fetch● POST append/process● PUT create/update● DELETE delete● Uniform interface, GET always gets, PUT always
creates● Using different verbs for different nouns would make
widespread communication impossible● There are no applications you can think of which
cannot be made to fit
http://flickr.com/photos/joygant/971783023/
6
Having a shared set of media-types
● What's not machineprocessable about the current Web isn't the protocol, it's the content
● Information conveyed via documents● A standard set of document formats (HTML, RDF,
JPEG, PNG, etc.)● Representation of a resource● Resources are just concepts, representations are how
we interact with them
http://flickr.com/photos/thefrankfurtschool/1305454450/
7
The use of hypermedia for application state-transitions
● Hypertext provides links between resources● Clients change state (navigate the Web) via information
from a previous state● URLs are opaque to clients, they never construct URLs● Because the links mirror the structure of how a user
makes progress through an application● A Webbased application is a dynamically changing
graph of state representations (pages) and potential transitions (links) between states
● If not, it may be accessible from the Web, but it’s not really part of the Web
http://flickr.com/photos/pgoyette/100769956/
8
Statelessprotocol
● Application state is the information necessary to understand the context of an interaction – auth details, etc.
● Resource state – S in REST, avoid unnamed state● All requests must include all application state● Session state is application state – if you want a
session you need a smarter client than a browser – shopping cart
● Prevents partial failures● Loadbalancing● Service interruptions
http://flickr.com/photos/davenyc/23033147/
9
Building a RESTful App
● Discover first class objects● Our resources● Assign URLs and URLspaces● Define representations● Input and output formats● Define HTTP methods
Time for an Example
http://flickr.com/photos/hugovk/2037935886/
10
RESTfully Delicious
● Get a list of all bookmarks● filter by user and/or tag● limit by number
● Add a bookmark● Edit a bookmark● Delete a bookmark
Get a list of all bookmarksfilter by user and/or taglimit by number
Add a bookmarkEdit a bookmarkDelete a bookmark
http://flickr.com/photos/sharynmorrow/124428600/
11
Discover Resources
Resources URLs Methods RepresentationsBookmark /bookmarks/{md5} GET
PUTDELETE
Bookmark list /bookmarks GETPOST
Users bookmarks /users/{user} GETTagged bookmarks /tags/{tag} GET
application/bookmark+xmlapplication/bookmark+xml
application/atom+xmlapplication/bookmark+xmlapplication/atom+xmlapplication/atom+xml
Resources URLs Methods RepresentationsUser list /users GETTag list /tags GETHomepage / GET
application/atom+xmlapplication/atom+xmlapplication/delicious+xml
http://flickr.com/photos/sharynmorrow/124428600/
12
GETting the Homepage
GET /
200 OK
Contenttype: application/delicious+xml
<?xml version=”1.0”?>
<delicious users=”/users” bookmarks=”/bookmarks” tags=”/tags”>
<recent start=”1” end=”20” next=”/?start=21&end=40”>
<bookmark url="http://www.example.org/somethingintersting" href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>
...
</recent>
</delicious>
http://flickr.com/photos/sharynmorrow/124428600/
13
GETting Bookmarks
GET /bookmarks
200 OK
Contenttype: application/atom+xml
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Bookmarks</title>
<entry>
<title>Something interesting</title>
<link href="/bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf"/>
<summary>http://example.org/somethingintersting</summary>
</entry>
</feed>
http://flickr.com/photos/sharynmorrow/124428600/
14
GETting A Bookmark
GET /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
200 OK
Contenttype: application/bookmark+xml
<?xml version="1.0"?>
<bookmark>
<title>Something interesting</title>
<url>http://example.org/somethingintersting</url>
<user href=”/users/pauljames”>pauljames</user>
<tags>
<tag href=”/tags/interesting”>interesting</tag>
</tags>
</bookmark>
http://flickr.com/photos/sharynmorrow/124428600/
15
Creating and Updating
POST /bookmarks
Contenttype: application/bookmark+xml
...
201 Created
Location: /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
PUT /bookmarks/a211528fb5108cddaa4b0d3aeccdbdcf
Contenttype: application/bookmark+xml
...
200 OK
http://flickr.com/photos/sharynmorrow/124428600/
16
Benefits of Being RESTful
● Better scaling due to stateless communications ● Better response times due to caching● Better longterm compatibility due to:
● the capability of document types to evolve without breaking backwardscompatibility
● the ability to add support for new content types without reducing support for older content types.
● Do less with more
Better scaling due to stateless communications Better response times due to cachingBetter longterm compatibility due to:
the capability of document types to evolve without breaking backwardscompatibility
the ability to add support for new content types without reducing support for older content types.
Lower learning curve for consumerLower support overhead for producer
http://flickr.com/photos/ari/1387533615/
17
Finally, some PHP
HTTP method$_SERVER['REQUEST_METHOD']
Reading request data$_SERVER['CONTENT_LENGTH']
$_SERVER['CONTENT_TYPE']
POST - $HTTP_RAW_POST_DATA
Other HTTP methods - fopen('php://input', 'r')
Processing request dataSimpleXML, MiniXML, PHP JSON, parse_str, unserialize,
unpack, preg_match, etc.
http://flickr.com/photos/nez/378348754/
18
Generating ResponsesHTTP response codes
200 OK, 201 Created, 204 No Content, 304 Not Modified, 401 Unauthorized, 404 Not Found, 405 Method Not Allowed, 411 Length Required, 415 Unsupported Media Type
Generating representationsSmarty, PHPFastTemplate, GD, SimpleXML, printf, etc.
HTTP cachingExpires, Cache-Control, Etag, Last-Modifiedheader('Expires: '.gmdate('D, j M Y H:i:s T', time() + $cachelength));
header('CacheControl: maxage='.$cachelength.', mustrevalidate');
http://flickr.com/photos/nez/378348754/
19
Conclusion
● REST is not just about “Web Services”● Set of best practices for building Web apps
● Give everything a URL● Use correct HTTP methods● Use common media types● Link things together with hypertext
REST is not just about “Web Services”Set of best practices for building Web apps
Give everything a URLUse correct HTTP methodsUse common media typesLink things together with hypertext
REST is an architectural styleIt defines 4 core principles
A universal syntax for resource-identification
A set of well-defined operationsHaving a shared set of media-typesThe use of hypermedia for application
state-transitionsStateless client/server interaction
It helps us write well behaved appsIt's not just about Web APIs but the
whole Web, “there are no Web Services”
http://flickr.com/photos/naked_dave1/469220787/
20
Further Reading
● Roger L. Costello (http://www.xfront.com/RESTWebServices.html)
● Paul Prescod (http://www.prescod.net/rest/)● REST Wiki (http://rest.blueoxen.net/)● Atom Publishing Protocol
(http://atomenabled.org/developers/protocol/)● http://del.icio.us/tag/rest
http://flickr.com/photos/dhammza/91435718/