SlideShare une entreprise Scribd logo
1  sur  45
Télécharger pour lire hors ligne
AmsterdamPHP



               Create a RESTful api
                        November 15, 2012
AmsterdamPHP
INTRODUCTION




                   Marc Ypes
                       @Ceeram


                   CakePHP 4 years
                  Core team 1.5 years
               Undercover as programmer


AmsterdamPHP
OVERVIEW


 ■   Content-types
 ■   Interface
 ■   Authentication
 ■   Cache
 ■   Errors




AmsterdamPHP
INTRODUCTION TO REST
 Representational state transfer

 Set of architectural principles

 - resource focussed
 - manipulation through representations
 - HTTP protocol?




AmsterdamPHP
INTRODUCTION TO REST
 Constraints

 ■   Client-server
 ■   Stateless
 ■   Uniform interface
 ■   Cacheable
 ■   Layered system




AmsterdamPHP
INTRODUCTION TO REST
 Client-server

 separation of concerns

 portability of UI across platforms, scalability

 allowing components to evolve independently




AmsterdamPHP
INTRODUCTION TO REST
 Stateless

 request includes required information

 no stored context on server: Sessions




AmsterdamPHP
INTRODUCTION TO REST
 Uniform interface

 information is transfered in standardized form

 using nouns for resources

 (http) protocol describes methods




AmsterdamPHP
INTRODUCTION TO REST
 Cacheable

 reducing latency

 lower serverload




AmsterdamPHP
INTRODUCTION TO REST
 Layered system

 Server
 Client

 Proxy
 Gateway
 Security
 Anything



AmsterdamPHP
INTRODUCTION TO REST
 Uniform interface

 ■ resource

 ■ identification of the resource

 ■ manipulation through representation

 ■ self-descriptive

 ■ hypermedia as the engine of application state
   HATEOAS
AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 Data element                Example
 resource                    user, book etc. (users, books etc.)
 resource identifier         URL, URN (/users/1234)
 representation
    data                     TXT / HTML / XML /YAML,JSON
    metadata                 content type, last-modified time
 resource metadata           source link, alternate
 control data                if-modified-since, cache-control, etag


 http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm


AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 /api/getUserProfile/1234

 /api/users?action=vote&id=1234

 /api/deleteUser?id=1




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 RPC style

 Steep learning curve
 Documentation
 New functionality, BC




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 2 base urls

 - /books
 - /books/1234




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 GET           /users        Get collection
 POST          /users        Add to collection
 GET           /users/1234   Get resource
 PUT           /users/1234   Update resource
 DELETE        /users/1234   Delete resource

 Update is not replace?
 POST       /users/1234

AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 Typical request:

 >GET /books/1849511926 HTTP/1.1
 >Host: api.amazin.com
 >Accept: application/json

 >If-Modified-Since: Sat, 01 Sep 2012 10:22:36 GMT



AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 Typical response:

 < HTTP/1.1 200 OK
 < Date: Sat, 01 Sep 2012 11:45:12 GMT
 < Server: Apache/2.2.16 (Debian)
 < Last-Modified: Sat, 01 Sep 2012 11:25:31 GMT
 < Content-Length: 145
 < Content-Type: application/json
 {"book":{........"}}
AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 Typical response:

 < HTTP/1.1 304 Not Modified
 < Date: Sat, 01 Sep 2012 11:45:12 GMT
 < Server: Apache/2.2.16 (Debian)
 < Vary: Accept-Encoding




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 Safe methods
 Idempotent methods

 GET (HEAD) is safe (nullipotent)
 PUT, DELETE are idempotent

 POST
 PATCH?

AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 Normalize the resources

 GET           /books/1849511926/votes

 GET           /votes?book=1849511926




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 Normalize the resources

 POST          /books/1849511926/votes

 PUT          /books/1849511926
 data contains votes subresource data

 POST         /votes
 data is book=1849511926

AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface

 PATCH

 Edge Rails: PATCH is the new primary HTTP method for updates




 http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/



AmsterdamPHP
INTRODUCTION TO REST
 Versioning

 /rest
 /v1

 content-type




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS

 Level 3 of REST maturity model (RMM)




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS

 Level 0

 Single URI, single HTTP method




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS

 Level 1

 Many URI, single HTTP method




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS

 Level 2

 Many URI, different HTTP methods




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS

 Level 3

 Self descriptive
 ■ Media types
 ■ Links
 ■ Other protocols




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS

 HATEOAS

 GET           /comments?book=1849511926

 Link to api.amazin.com/books/1849511926

 Links to all comments


AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS
 >GET      /comments/1
 <HTTP/1.1 200 Ok
 <Content-Type: text/xml
 <?xml version="1.0">
 <comment>
    <foo>great book</foo>
    <book>
       <link href="/books/1849511926" title="wow" />
    </book>
 </comment>
AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS
     {
         "foo":"great book",
         "book":
             {
                 "links":[
                     {
                          "href":"/books/1849511926",
                          "title":"wow"
                     }
                 ]
             }
     }
AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / HATEOAS

 Link header

 Link: <https://api.github.com/user/repos?page=2&per_page=100>;rel="
 next",
  <https://api.github.com/user/repos?page=50&per_page=100>;rel="last"




                               github.com
AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / Errors

 HTTP Statuscodes

 Human reads message
 Code reads code




AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / Errors

 HTTP Statuscodes

 200 OK
 201 Created
 204 No Content
 304 Not Modified
 400 Bad Request
 401 Unauthorized
 404 Not Found
 405 Method Not Allowed
AmsterdamPHP
INTRODUCTION TO REST
 Uniform Interface / Errors

 Link to support page

 Link: <http://api.amazin.com/errors/405>; rel="help"




AmsterdamPHP
REST my Cake
 Authentication

 Public
 Api-key
 Basic Auth
 OAuth




CakeFest 2012 Manchester
INTRODUCTION TO REST
 Cacheable

 HTTP Cache headers

 Use them!




AmsterdamPHP
INTRODUCTION TO REST
 Cacheable

 HTTP Cache headers

 Cache-control
 - private
 - public
 - max-age / s-maxage
 - must-revalidate
 - nocache


AmsterdamPHP
INTRODUCTION TO REST
 Cacheable

 HTTP Cache validation

 Etag                    Response
 Last-Modified           Response

 If-Modified-Since       Request (GET)
 If-None-Match           Request (GET)

 If-Match                Request (PUT)

AmsterdamPHP
REST my Cake
 CakeResponse

 header( $header = NULL, $value = NULL )
 send( )
 statusCode( $code = NULL )
 type( $contentType = NULL )




AmsterdamPHP
REST my Cake
 CakeResponse

 cache( $since, $time = '+1 day' )
 checkNotModified( $request )
 disableCache( )
 etag( $tag = NULL, $weak = false )
 expires( $time = NULL )
 maxAge( $seconds = NULL )
 modified( $time = NULL )
 mustRevalidate( $enable = NULL )
 notModified( )
 sharable( $public = NULL, $time = NULL )
 sharedMaxAge( $seconds = NULL )
AmsterdamPHP
REST my Cake

 CakePHP 3.0

 2013
 PHP 5.4
 composer
 arrays => objects




AmsterdamPHP
INTRODUCTION TO REST


 REST might not be what you are looking for

 Questions?




AmsterdamPHP
INTRODUCTION TO REST




                     THANKS

               https://joind.in/event/view/1141

AmsterdamPHP

Contenu connexe

Tendances

JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5
Stephan Schmidt
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
Bo-Yi Wu
 

Tendances (20)

Crash course to the Apache Camel
Crash course to the Apache CamelCrash course to the Apache Camel
Crash course to the Apache Camel
 
JSON-RPC - JSON Remote Procedure Call
JSON-RPC - JSON Remote Procedure CallJSON-RPC - JSON Remote Procedure Call
JSON-RPC - JSON Remote Procedure Call
 
Web services tutorial
Web services tutorialWeb services tutorial
Web services tutorial
 
JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5JSON-RPC Proxy Generation with PHP 5
JSON-RPC Proxy Generation with PHP 5
 
PHP and Web Services
PHP and Web ServicesPHP and Web Services
PHP and Web Services
 
SOAP-based Web Services
SOAP-based Web ServicesSOAP-based Web Services
SOAP-based Web Services
 
RESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
 
Os Furlong
Os FurlongOs Furlong
Os Furlong
 
Wizard of ORDS
Wizard of ORDSWizard of ORDS
Wizard of ORDS
 
Tomcat Server
Tomcat ServerTomcat Server
Tomcat Server
 
Os Pruett
Os PruettOs Pruett
Os Pruett
 
Building RESTful applications using Spring MVC
Building RESTful applications using Spring MVCBuilding RESTful applications using Spring MVC
Building RESTful applications using Spring MVC
 
Reaching Out From PL/SQL (OPP 2010)
Reaching Out From PL/SQL (OPP 2010)Reaching Out From PL/SQL (OPP 2010)
Reaching Out From PL/SQL (OPP 2010)
 
The Functional Web
The Functional WebThe Functional Web
The Functional Web
 
Xml Publisher And Reporting To Excel
Xml Publisher And Reporting To ExcelXml Publisher And Reporting To Excel
Xml Publisher And Reporting To Excel
 
Web services - A Practical Approach
Web services - A Practical ApproachWeb services - A Practical Approach
Web services - A Practical Approach
 
Introduction to RESTful Web Services
Introduction to RESTful Web ServicesIntroduction to RESTful Web Services
Introduction to RESTful Web Services
 
Power Shell and Sharepoint 2013
Power Shell and Sharepoint 2013Power Shell and Sharepoint 2013
Power Shell and Sharepoint 2013
 
Red5 - PHUG Workshops
Red5 - PHUG WorkshopsRed5 - PHUG Workshops
Red5 - PHUG Workshops
 
Introduction to asp.net web api
Introduction to asp.net web apiIntroduction to asp.net web api
Introduction to asp.net web api
 

Similaire à Amsterdam php create a restful api

Cake fest 2012 create a restful api
Cake fest 2012 create a restful apiCake fest 2012 create a restful api
Cake fest 2012 create a restful api
ceeram
 
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Nguyen Duc Phu
 
Restful web services rule financial
Restful web services   rule financialRestful web services   rule financial
Restful web services rule financial
Rule_Financial
 
nguyenhainhathuy-building-restful-web-service
nguyenhainhathuy-building-restful-web-servicenguyenhainhathuy-building-restful-web-service
nguyenhainhathuy-building-restful-web-service
hazzaz
 

Similaire à Amsterdam php create a restful api (20)

Cake fest 2012 create a restful api
Cake fest 2012 create a restful apiCake fest 2012 create a restful api
Cake fest 2012 create a restful api
 
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
Hanoi php day 2008 - 05. nguyen hai nhat huy - building-restful-web-service-w...
 
Web api
Web apiWeb api
Web api
 
Intro to Hypermedia APIs
Intro to Hypermedia APIsIntro to Hypermedia APIs
Intro to Hypermedia APIs
 
Fulfilling the Hypermedia Constraint via HTTP OPTIONS, The HTTP Vocabulary In...
Fulfilling the Hypermedia Constraint via HTTP OPTIONS, The HTTP Vocabulary In...Fulfilling the Hypermedia Constraint via HTTP OPTIONS, The HTTP Vocabulary In...
Fulfilling the Hypermedia Constraint via HTTP OPTIONS, The HTTP Vocabulary In...
 
RESTful Web Services with JAX-RS
RESTful Web Services with JAX-RSRESTful Web Services with JAX-RS
RESTful Web Services with JAX-RS
 
REST-API introduction for developers
REST-API introduction for developersREST-API introduction for developers
REST-API introduction for developers
 
Apex REST
Apex RESTApex REST
Apex REST
 
Restful web services rule financial
Restful web services   rule financialRestful web services   rule financial
Restful web services rule financial
 
ITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul ServicesITB2016 - Building ColdFusion RESTFul Services
ITB2016 - Building ColdFusion RESTFul Services
 
ASP.NET WEB API Training
ASP.NET WEB API TrainingASP.NET WEB API Training
ASP.NET WEB API Training
 
L18 REST API Design
L18 REST API DesignL18 REST API Design
L18 REST API Design
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 
nguyenhainhathuy-building-restful-web-service
nguyenhainhathuy-building-restful-web-servicenguyenhainhathuy-building-restful-web-service
nguyenhainhathuy-building-restful-web-service
 
Great APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGreat APIs - Future of Your Progress App
Great APIs - Future of Your Progress App
 
Introduction to REST and Hypermedia
Introduction to REST and HypermediaIntroduction to REST and Hypermedia
Introduction to REST and Hypermedia
 
HTTP Basics Demo
HTTP Basics DemoHTTP Basics Demo
HTTP Basics Demo
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rs
 
REST-API overview / concepts
REST-API overview / conceptsREST-API overview / concepts
REST-API overview / concepts
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Dernier

Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 

Dernier (20)

A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Your enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4jYour enemies use GenAI too - staying ahead of fraud with Neo4j
Your enemies use GenAI too - staying ahead of fraud with Neo4j
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 

Amsterdam php create a restful api

  • 1. AmsterdamPHP Create a RESTful api November 15, 2012 AmsterdamPHP
  • 2. INTRODUCTION Marc Ypes @Ceeram CakePHP 4 years Core team 1.5 years Undercover as programmer AmsterdamPHP
  • 3. OVERVIEW ■ Content-types ■ Interface ■ Authentication ■ Cache ■ Errors AmsterdamPHP
  • 4. INTRODUCTION TO REST Representational state transfer Set of architectural principles - resource focussed - manipulation through representations - HTTP protocol? AmsterdamPHP
  • 5. INTRODUCTION TO REST Constraints ■ Client-server ■ Stateless ■ Uniform interface ■ Cacheable ■ Layered system AmsterdamPHP
  • 6. INTRODUCTION TO REST Client-server separation of concerns portability of UI across platforms, scalability allowing components to evolve independently AmsterdamPHP
  • 7. INTRODUCTION TO REST Stateless request includes required information no stored context on server: Sessions AmsterdamPHP
  • 8. INTRODUCTION TO REST Uniform interface information is transfered in standardized form using nouns for resources (http) protocol describes methods AmsterdamPHP
  • 9. INTRODUCTION TO REST Cacheable reducing latency lower serverload AmsterdamPHP
  • 10. INTRODUCTION TO REST Layered system Server Client Proxy Gateway Security Anything AmsterdamPHP
  • 11. INTRODUCTION TO REST Uniform interface ■ resource ■ identification of the resource ■ manipulation through representation ■ self-descriptive ■ hypermedia as the engine of application state HATEOAS AmsterdamPHP
  • 12. INTRODUCTION TO REST Uniform Interface Data element Example resource user, book etc. (users, books etc.) resource identifier URL, URN (/users/1234) representation data TXT / HTML / XML /YAML,JSON metadata content type, last-modified time resource metadata source link, alternate control data if-modified-since, cache-control, etag http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm AmsterdamPHP
  • 13. INTRODUCTION TO REST Uniform Interface /api/getUserProfile/1234 /api/users?action=vote&id=1234 /api/deleteUser?id=1 AmsterdamPHP
  • 14. INTRODUCTION TO REST Uniform Interface RPC style Steep learning curve Documentation New functionality, BC AmsterdamPHP
  • 15. INTRODUCTION TO REST Uniform Interface 2 base urls - /books - /books/1234 AmsterdamPHP
  • 16. INTRODUCTION TO REST Uniform Interface GET /users Get collection POST /users Add to collection GET /users/1234 Get resource PUT /users/1234 Update resource DELETE /users/1234 Delete resource Update is not replace? POST /users/1234 AmsterdamPHP
  • 17. INTRODUCTION TO REST Uniform Interface Typical request: >GET /books/1849511926 HTTP/1.1 >Host: api.amazin.com >Accept: application/json >If-Modified-Since: Sat, 01 Sep 2012 10:22:36 GMT AmsterdamPHP
  • 18. INTRODUCTION TO REST Uniform Interface Typical response: < HTTP/1.1 200 OK < Date: Sat, 01 Sep 2012 11:45:12 GMT < Server: Apache/2.2.16 (Debian) < Last-Modified: Sat, 01 Sep 2012 11:25:31 GMT < Content-Length: 145 < Content-Type: application/json {"book":{........"}} AmsterdamPHP
  • 19. INTRODUCTION TO REST Uniform Interface Typical response: < HTTP/1.1 304 Not Modified < Date: Sat, 01 Sep 2012 11:45:12 GMT < Server: Apache/2.2.16 (Debian) < Vary: Accept-Encoding AmsterdamPHP
  • 20. INTRODUCTION TO REST Uniform Interface Safe methods Idempotent methods GET (HEAD) is safe (nullipotent) PUT, DELETE are idempotent POST PATCH? AmsterdamPHP
  • 21. INTRODUCTION TO REST Uniform Interface Normalize the resources GET /books/1849511926/votes GET /votes?book=1849511926 AmsterdamPHP
  • 22. INTRODUCTION TO REST Uniform Interface Normalize the resources POST /books/1849511926/votes PUT /books/1849511926 data contains votes subresource data POST /votes data is book=1849511926 AmsterdamPHP
  • 23. INTRODUCTION TO REST Uniform Interface PATCH Edge Rails: PATCH is the new primary HTTP method for updates http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/ AmsterdamPHP
  • 24. INTRODUCTION TO REST Versioning /rest /v1 content-type AmsterdamPHP
  • 25. INTRODUCTION TO REST Uniform Interface / HATEOAS Level 3 of REST maturity model (RMM) AmsterdamPHP
  • 26. INTRODUCTION TO REST Uniform Interface / HATEOAS Level 0 Single URI, single HTTP method AmsterdamPHP
  • 27. INTRODUCTION TO REST Uniform Interface / HATEOAS Level 1 Many URI, single HTTP method AmsterdamPHP
  • 28. INTRODUCTION TO REST Uniform Interface / HATEOAS Level 2 Many URI, different HTTP methods AmsterdamPHP
  • 29. INTRODUCTION TO REST Uniform Interface / HATEOAS Level 3 Self descriptive ■ Media types ■ Links ■ Other protocols AmsterdamPHP
  • 30. INTRODUCTION TO REST Uniform Interface / HATEOAS HATEOAS GET /comments?book=1849511926 Link to api.amazin.com/books/1849511926 Links to all comments AmsterdamPHP
  • 31. INTRODUCTION TO REST Uniform Interface / HATEOAS >GET /comments/1 <HTTP/1.1 200 Ok <Content-Type: text/xml <?xml version="1.0"> <comment> <foo>great book</foo> <book> <link href="/books/1849511926" title="wow" /> </book> </comment> AmsterdamPHP
  • 32. INTRODUCTION TO REST Uniform Interface / HATEOAS { "foo":"great book", "book": { "links":[ { "href":"/books/1849511926", "title":"wow" } ] } } AmsterdamPHP
  • 33. INTRODUCTION TO REST Uniform Interface / HATEOAS Link header Link: <https://api.github.com/user/repos?page=2&per_page=100>;rel=" next", <https://api.github.com/user/repos?page=50&per_page=100>;rel="last" github.com AmsterdamPHP
  • 34. INTRODUCTION TO REST Uniform Interface / Errors HTTP Statuscodes Human reads message Code reads code AmsterdamPHP
  • 35. INTRODUCTION TO REST Uniform Interface / Errors HTTP Statuscodes 200 OK 201 Created 204 No Content 304 Not Modified 400 Bad Request 401 Unauthorized 404 Not Found 405 Method Not Allowed AmsterdamPHP
  • 36. INTRODUCTION TO REST Uniform Interface / Errors Link to support page Link: <http://api.amazin.com/errors/405>; rel="help" AmsterdamPHP
  • 37. REST my Cake Authentication Public Api-key Basic Auth OAuth CakeFest 2012 Manchester
  • 38. INTRODUCTION TO REST Cacheable HTTP Cache headers Use them! AmsterdamPHP
  • 39. INTRODUCTION TO REST Cacheable HTTP Cache headers Cache-control - private - public - max-age / s-maxage - must-revalidate - nocache AmsterdamPHP
  • 40. INTRODUCTION TO REST Cacheable HTTP Cache validation Etag Response Last-Modified Response If-Modified-Since Request (GET) If-None-Match Request (GET) If-Match Request (PUT) AmsterdamPHP
  • 41. REST my Cake CakeResponse header( $header = NULL, $value = NULL ) send( ) statusCode( $code = NULL ) type( $contentType = NULL ) AmsterdamPHP
  • 42. REST my Cake CakeResponse cache( $since, $time = '+1 day' ) checkNotModified( $request ) disableCache( ) etag( $tag = NULL, $weak = false ) expires( $time = NULL ) maxAge( $seconds = NULL ) modified( $time = NULL ) mustRevalidate( $enable = NULL ) notModified( ) sharable( $public = NULL, $time = NULL ) sharedMaxAge( $seconds = NULL ) AmsterdamPHP
  • 43. REST my Cake CakePHP 3.0 2013 PHP 5.4 composer arrays => objects AmsterdamPHP
  • 44. INTRODUCTION TO REST REST might not be what you are looking for Questions? AmsterdamPHP
  • 45. INTRODUCTION TO REST THANKS https://joind.in/event/view/1141 AmsterdamPHP