by Enrico Zimuel - Twitter: @ezimuel
Senior Sofware Engineer, Zend Technologies Ltd
28th March - Rome (Italy)
Professional Developer since 1996. Senior Software Engineer at Zend Technologies, since 2008. Research in computer science at the Informatics Institute of Amsterdam University. Open source contributor and co-author of Apigility and Zend Framework. Co-author of the Italian books PHP Best Practices and Javascript Best Practices. Co-founder of PUG Torino (Italy).
API stands for "Application Programming Interface" and as a term, specifies how software should interact.
When we refer to APIs today, we are referring more specifically to web APIs, those delivered over HyperText Transfer Protocol (HTTP).
REpresentational State Transfer (REST) is an architecture designed around the HTTP specification.
Talking about REST, the Richardson Maturity Model is often used to describe the concerns necessary when implementing a well-designed REST API
The usage of HTTP as communication layer
A format for data representation (e.g. JSON)
Basically a Remote Procedure Call (RPC)
URIs as unique identifiers for resources
For instance, the resource User can be identified by
http://domain/api/user[/:user_id]
where user_id is an optional parameter
Usage of HTTP verbs for operations on resources
CRUD | HTTP verbs |
CREATE | POST |
READ | GET |
UPDATE | PUT, PATCH* |
DELETE | DELETE |
* partial update
Linking between resources to indicate relationships (hypermedia)
GET /api/user/ezimuel
{
"_links": {
"self": {
"href": "http://domain/api/user/ezimuel"
},
"contacts": [
{ "href": "http://domain/api/user/mwop" },
{ "href": "http://domain/api/user/zeevs" }
]
},
"id": "ezimuel",
"name": "Enrico Zimuel"
}
JSON-HAL format
Hypermedia As The Engine Of Application State
Scalable architecture
Very easy to consume
Reduce client/server coupling
Discoverability
$data = array(
'id' => 'ezimuel',
'name' => 'Enrico Zimuel'
);
header('Content-Type: application/json');
echo json_encode($data);
Quite simple, right?
No! What about error handling, hypermedia, data validation, content negotiation, versioning, etc?
Can be difficult, especially at the beginning
GET /api/user/ezimuel
{
"_links": {
"self": {
"href": "http://domain/api/user/ezimuel"
}
}
"id": "ezimuel",
"name": "Enrico Zimuel"
}
{
"_links": {
"self": {
"href": "http://domain/api/user/ezimuel"
}
}
"id": "ezimuel",
"name": "Enrico Zimuel",
"_embedded": {
"contacts": [
{
"_links": {
"self": {
"href": "http://domain/api/user/mwop"
}
},
"id": "mwop",
"name": "Matthew Weier O'Phinney"
},
{
"_links": {
"self": {
"href": "http://domain/api/user/zeevs"
}
},
"id": "zeevs",
"name": "Zeev Suraski"
}
]
}
}
{
"_links": {
"self": {
"href": "http://domain/api/user?page=3"
},
"first": {
"href": "http://domain/api/user"
},
"prev": {
"href": "http://domain/api/user?page=2"
},
"next": {
"href": "http://domain/api/user?page=4"
},
"last": {
"href": "http://domain/api/user?page=133"
}
}
"count": 3,
"total": 498,
"_embedded": {
"users": [
{
"_links": {
"self": {
"href": "http://domain/api/user/mwop"
}
},
"id": "mwop",
"name": "Matthew Weier O'Phinney"
},
{
"_links": {
"self": {
"href": "http://domain/api/user/mac_nibblet"
}
},
"id": "mac_nibblet",
"name": "Antoine Hedgecock"
},
{
"_links": {
"self": {
"href": "http://domain/api/user/spiffyjr"
}
},
"id": "spiffyjr",
"name": "Kyle Spraggs"
}
]
}
}
Content-Type: application/problem+json
{
"detail": "The GET method has not been defined for individual",
"status": 405,
"title": "Method Not Allowed",
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"
}
Accept: application/hal+json, application/json
Agility uses two approaches:
v1
/userv1
+json Apigility supports three different authentication systems: HTTP Basic, HTTP Digest, and OAuth2
Super easy, just one command:
curl -sS https://apigility.org/install | php
Or, if you don't have CURL installed:
php -r "readfile('https://apigility.org/install');" | php
Open the browser to http://localhost:8888
Preview of the new UI of Apigility 1.1
Rate this talk: https://joind.in/14149
More information on apigility.org
Contact me: enrico [at] zend.com
Follow me: @ezimuel
This work is licensed under a
Creative Commons Attribution-ShareAlike 3.0 Unported License.
I used reveal.js to make this presentation.