This document discusses building REST APIs. It begins with introductions and then provides an overview of REST including:
- REST is based on HTTP and uses common methods like GET, POST, PUT, DELETE.
- Resources are represented as URIs and can be acted on via HTTP methods rather than function calls.
- Responses use standard HTTP response codes to indicate success or errors.
- Authentication is typically done with tokens rather than cookies to maintain statelessness.
- The Symfony framework is well-suited for building REST APIs in PHP due to its maturity and object-oriented architecture.
2. Who are you?
@AlmogBaku nice to meet ya`
1. Entrepreneur
2. Co-Founder & CTO @ Rimoto
3. Developer for 12 years
4. GitHub addicted.
5. Blog about entrepreneurship and development:
www.AlmogBaku.com
3. What are we going to talk about?
● What tha’ heck is REST?
● Differences between SOAP and REST
● Authentication methods
● Symfony2 and REST
11. The old days: web services
A Web service is a method of communication between two electronic devices
over a network.
Created by and for enterprises.
Makes data available as services (verb + noun), for example “getAuthor” or
“PayInvoice”:
GetAuthor(183);
12. The old days: web services
Request:
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/author">
<m:GetAuthor>
<m:Id>183</m:Id>
</m:GetAuthor>
</soap:Body>
</soap:Envelope>
13. The old days: web services
Response:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/author">
<m:GetAuthor>
<m:Id>183</m:Id>
<m:Name>J.R.R. Tolkien</m:Name>
<m:Birthday>1-3-1892</m:Birthday>
</m:GetAuthor>
</soap:Body>
</soap:Envelope>
15. REST
REST is just the regular way the internet works!
GET http://google.com/
16. REST
REST is just the regular way the internet works!
GET http://google.com/RESPONSE 200 OK
17. REST protocol features
1. Follows the philosophy of Open Web
2. Easy to use (common standard)
3. Based on the HTTP protocol
4. Uses http status codes
5. Resource oriented
6. Response can be describe in JSON too!
7. Cacheable
8. Stateless
18. REST
REST is about resources, not about functions.
Book store api:
1. /api/authors/
2. /api/authors/:authorId/
3. /api/authors/:authorId/books/
4. /api/authors/:authorId/books/:bookId
5. /api/authors/:authorId/books/:bookId/reviews
6. /api/authors/:authorId/books/:bookId/reviews/:reviewId
19. REST
REST is about resources, not about functions.
Book store api:
1. /api/authors/
2. /api/authors/:authorId/
3. /api/authors/:authorId/books/
4. /api/authors/:authorId/books/:bookId
5. /api/authors/:authorId/books/:bookId/reviews
6. /api/authors/:authorId/books/:bookId/reviews/:reviewId
21. REST
REST is about resources, not about functions.
Book store api:
1. /api/authors/
2. /api/authors/:authorId/
3. /api/authors/:authorId/books/
4. /api/authors/:authorId/books/:bookId
5. /api/authors/:authorId/books/:bookId/reviews
6. /api/authors/:authorId/books/:bookId/reviews/:reviewId
23. REST
The same URIs can do many different actions...
We can request web pages in one of the following methods:
1. GET - request information about resource
2. POST - create new resource
3. PUT - update resource
4. DELETE - delete resource
5. HEAD - get information only with headers (eg. if resource exists)
6. OPTIONS - list of available methods to the resource (like --help)
24. REST
Errors are simple http errors
200 - OK
201 - Resource created
404 - Not found
401 - Unauthorized
500 - Server Error
Etc.
25. REST
REST is Stateless
- You can’t use cookies
- You need to pass your identification in every request
- Basic access authentication (username/password)
- Tokenized authentication (like OAuth, JWT, etc)
GET /users/me?access_token=ftjhi89uh5982hbrvt92vgt9qvhg2r0219
27. Symfony
1. Popular PHP framework
2. Very mature (since 2004)
3. Very Object Oriented architecture
4. Designed for high performance
5. Leading the PHP world, any used in many
other popular projects