Andrew Curioso
Twitter: @AndrewCurioso
#nephp
Pragmatic API Development
Andrew Curioso
What is an API?
● Application Programming Interface
● Web Service
● Contract
Business Case
● Mobile API First
Business Case
● Multiple Devices
Your API
Become a Platform
● Internal
– Multi-platform
– Scalable
● Semi-Public
– Partner Integration
● Public
– Everything+
– Growth
● Mashups
● Innovation
● Evangelism
– “The Platform Play”
Types of APIs
● Representation StateTransfer (REST)
● Remote Procedure Call (RPC)
● Realtime Streaming
What is REST?
● Nouns
● Verbs
● HTTP is inherently RESTful
Why REST
● Intuitive
● Easy to implement in PHP
● Widely accepted
Nouns
● Resources
– Blog post
– User
– Etc.
● Unique URIs
– Example: http://www.example.com/users/1234.json
Verbs
● 5 most common
– GET
– POST
– PUT
– DELETE
– HEAD
Verbs
● Uses
– GET – Read data
– POST and PUT write data
– DELETE and POST delete data
CSRF
● Cross Site Request Forgery
● Vectors:
– Embedded resources (<img>, <script>, etc)
– Hidden forms
security
CSRF
● Never edit data with GET
– Ajax Same origin policy→
● Forms
– Single use tokens
– Referrer check
security
Response Formats
● Json
– Very common for REST APIs
– Simple
– Fast
– Multi-platform
Response Formats
● JsonP
– P Padding→
– Allows reading data cross origin
– GET only
Response Formats
● XML
– Heavy and verbose
– Strictly typed
– Lots of existing tools
Response Formats
● Text
– Json / JsonP
– XML
– HTML
– YAML
– CSV
– Serialized PHP
– Etc.
● Binary
– Microsoft Excel
– PDF
– JPG / PNG / GIF
– MP3
– Etc.
Status Codes
● Success
– 200 OK
– 201 Created
– 301 Permanent Redirect
– 302 Found
– 303 See Other
Status Codes
● Error
– 401 Unauthorized
– 402 Payment Required
– 403 Forbidden
– 404 Not Found
– 405 Method NotAllowed
– 409 Conflict
– 410 Gone
– 500 Internal Server Error
– 501 Not Implemented
– 503 Service Unavailable
Status Codes
● Novelty
– 418 I Am ATeapot
– 415 Unavailable For Legal Reasons
Putting it together
● Getting
– Anything but GET
● 405 Method Not Allowed
– Resource not found
● 404 Not Found
– Success
● 200 OK
– Moved
● 301 Permanent Redirect or 302 Found
Putting it together
● Adding
– Anything but POST
● 405 Method Not Allowed
– Resource already existed
● 303 See Other
– Success
● 201 Created
– Error
● 500 Internal Server Error with description
Putting it together
● Editing
– Anything but PUT or POST
● 405 Method Not Allowed
– Resource does not exist
● 404 Not Found
– Success
● 200 OK
– Error
● 500 Internal Server Error with description
Putting it together
● Deleting
– Anything but DELETE or POST
● 405 Method Not Allowed
– Resource does not exist
● 404 Not Found
– Success
● 200 OK or 204 No Content
– Error
● 500 Internal Server Error with description
Putting it together
● Everything
– Not logged in
● 401 Unauthorized
– Logged in but permission denied
● 403 Forbidden
Putting it together
● Why POST or DELETE/PUT
– Client support
– _method=VERB
● /users.json?_method=POST
Example
● Past bin
● Simple
– No security
Example
● Making it RESTful
1) Identify nouns
2) Write down URI structure
3) Map verbs to the URIs
Example
● GET /documents.json
● POST /documents.json
● GET /documents/{id}.json
● DELETE /documents/{id}.json
● PUT /documents/{id}.json
HATEOAS
● Hypermedia as the Engine of Application
State
– Next state
– Content Negotiation
● Accepts header
Versioning
● URI
● Custom header
● Accepts header
Pagination
● Meta info
– URI to next/prev page (HATEOAS)
– Total count
– Current page
● Methods
– Header
– Document
Errors
● Same format
● Descriptive
{ "error" : { "code" : 404,
"description" : "The resource could not be found",
"name" : "Not Found"
} }
Testing
● GET Web Browser→
● OS X / Linux
– curl
– Example:
curl -X DELETE “
http://www.example.com/document/123.json”
● Automated UnitTests
Authentication
● Who am I?
● Types:
– Basic
– Digest
– OAuth 1 & 2 – No 3rd
party password sharing!
– Cookies
– API keys
● 401 Unauthorized
Authorization
● Can I do that?
● 403 Forbidden
Documentation
● Vocabularies / schemas
● Examples:
– Input
– Output
– Code
● Feedback
● WSDL 2.0
OtherTypes
● RPC
● Streaming
Checklist
✔ Design schema upfront
✔ Identify nouns
✔ Design URI structure
✔ Errors in same format
✔ Proper status codes
✔ Server-side validation
✔ Testing
✔ Documentation
Predictions
● Internet ofThings
● API Economy
● Success
Andrew Curioso
 Contact:
 www.AndrewCurioso.com/contact
 @AndrewCurioso onTwitter

NEPHP '13: Pragmatic API Development

Editor's Notes

  • #2 Thank you _____________ Today I’m going to be talking about creating a RESTful API with PHP. Not just any RESTful API, but an Epic one. &amp;lt;number&amp;gt;