Advanced Web Development in PHP Module VIII: Understanding REST API Rasan Samarasinghe ESOFT Computer Studies (pvt) Ltd. No 68/1, Main Street, Embilipitiya.
Contents 1. What is an API? 2. Comparing a website to an API 3. Classification of APIs 4. What is REST API? 5. What model does REST use? 6. REST HTTP Methods 7. HTTP Codes 8. The advantages of REST 9. What is CRUD? 10. CRUD Operations 11. CRUD Application Example 12. REST API Implementation 13. Folders and Files Structure 14. MySQL Database 15. Reading all Products 16. Reading one Product 17. Creating a Product 18. Updating a Product 19. Deleting a Product 20. Searching a Product
What is an API? • API stands for Application Programming Interface. • API’s basically allow your product or service to talk to another product or service. • Software-to-software interaction, not user interaction. • They are used to give people access to your data/resources from outside the firewall. • This means opening up your product’s data and functionality to other developers both internally and externally.
Comparing a website to an API
Compare GitHub Website https://github.com/rasansmn GitHub API https://api.github.com/users/rasansmn
Classification of APIs Web Service API • SOAP • XML-RPC and JSON-RPC • REST WebSocket APIs Library-based APIs • JavaScript • TWAIN Class-based APIs (Object Orientation) • Java API • Android API OS Functions and Routines • Access to file system • Access to user interface Object Remoting APIs • CORBA • .NET Remoting Hardware APIs • Video acceleration • Hard disk drives • PCI buses
What is REST API? • REST stands for Representational State Transfer. • It is a lighter weight alternative to SOAP and WSDL XML-based API protocols.
What model does REST use? • REST uses a client-server model, where the server is an HTTP server and the client sends HTTP verbs (GET, POST, PUT, DELETE), along with a URL and variable parameters that are URL-encoded. • The URL describes the object to act upon and the server replies with a result code and valid JavaScript Object Notation (JSON).
What model does REST use?
REST HTTP Methods Method Description GET Request to read a webpage HEAD Request to read a webpage’s header PUT Request to store a webpage POST Append to a names resource DELETE Remove the webpage TRACE Echo the incoming request CONNECT Reserved for future use OPTIONS Query certain options
HTTP Codes • 200 - “OK”. • 201 - “Created” (Used with POST). • 400 - “Bad Request” (Perhaps missing required parameters). • 401 - “Unauthorized” (Missing authentication parameters). • 403 - “Forbidden” (You were authenticated but lacking required privileges). • 404 - “Not Found”.
The advantages of REST • Separation between the client and the server. • Visibility, reliability and scalability. • The REST API is always independent of the type of platform or languages. • Lighter weight alternative to SOAP and WSDL XML-based API protocols.
What is CRUD? The CRUD acronym is often used to describe database operations. CRUD stands for CREATE, READ, UPDATE, and DELETE.
CRUD Operations • POST: A client wants to insert or create an object. • GET: A client wants to read an object. • PUT: A client wants to update an object. • DELETE: A client wants to delete an object.
CRUD Application Example
Simple REST API Implementation in PHP REST API objects and their operations • Products – Read (all records) – Read one – Create – Update – Delete – Search • Categories – Read (all records) – Other operations has to be implemented as an exercise
Web root Folders and Files Structure
MySQL Database Import MySQL database using api_db.sql dump file
Reading all Products Using PostMan as the client to read all products
Reading one Product Using PostMan as the client to read one product
Creating a Product Using PostMan as the client to create a product
Updating a Product Using PostMan as the client to update a product
Deleting a Product Using PostMan as the client to delete a product
Searching a Product Using PostMan as the client to search a product
The End http://twitter.com/rasansmn

Advanced Web Development in PHP - Understanding REST API

  • 1.
    Advanced Web Developmentin PHP Module VIII: Understanding REST API Rasan Samarasinghe ESOFT Computer Studies (pvt) Ltd. No 68/1, Main Street, Embilipitiya.
  • 2.
    Contents 1. What isan API? 2. Comparing a website to an API 3. Classification of APIs 4. What is REST API? 5. What model does REST use? 6. REST HTTP Methods 7. HTTP Codes 8. The advantages of REST 9. What is CRUD? 10. CRUD Operations 11. CRUD Application Example 12. REST API Implementation 13. Folders and Files Structure 14. MySQL Database 15. Reading all Products 16. Reading one Product 17. Creating a Product 18. Updating a Product 19. Deleting a Product 20. Searching a Product
  • 3.
    What is anAPI? • API stands for Application Programming Interface. • API’s basically allow your product or service to talk to another product or service. • Software-to-software interaction, not user interaction. • They are used to give people access to your data/resources from outside the firewall. • This means opening up your product’s data and functionality to other developers both internally and externally.
  • 4.
  • 5.
  • 6.
    Classification of APIs WebService API • SOAP • XML-RPC and JSON-RPC • REST WebSocket APIs Library-based APIs • JavaScript • TWAIN Class-based APIs (Object Orientation) • Java API • Android API OS Functions and Routines • Access to file system • Access to user interface Object Remoting APIs • CORBA • .NET Remoting Hardware APIs • Video acceleration • Hard disk drives • PCI buses
  • 7.
    What is RESTAPI? • REST stands for Representational State Transfer. • It is a lighter weight alternative to SOAP and WSDL XML-based API protocols.
  • 8.
    What model doesREST use? • REST uses a client-server model, where the server is an HTTP server and the client sends HTTP verbs (GET, POST, PUT, DELETE), along with a URL and variable parameters that are URL-encoded. • The URL describes the object to act upon and the server replies with a result code and valid JavaScript Object Notation (JSON).
  • 9.
    What model doesREST use?
  • 10.
    REST HTTP Methods MethodDescription GET Request to read a webpage HEAD Request to read a webpage’s header PUT Request to store a webpage POST Append to a names resource DELETE Remove the webpage TRACE Echo the incoming request CONNECT Reserved for future use OPTIONS Query certain options
  • 11.
    HTTP Codes • 200- “OK”. • 201 - “Created” (Used with POST). • 400 - “Bad Request” (Perhaps missing required parameters). • 401 - “Unauthorized” (Missing authentication parameters). • 403 - “Forbidden” (You were authenticated but lacking required privileges). • 404 - “Not Found”.
  • 12.
    The advantages ofREST • Separation between the client and the server. • Visibility, reliability and scalability. • The REST API is always independent of the type of platform or languages. • Lighter weight alternative to SOAP and WSDL XML-based API protocols.
  • 13.
    What is CRUD? TheCRUD acronym is often used to describe database operations. CRUD stands for CREATE, READ, UPDATE, and DELETE.
  • 14.
    CRUD Operations • POST:A client wants to insert or create an object. • GET: A client wants to read an object. • PUT: A client wants to update an object. • DELETE: A client wants to delete an object.
  • 15.
  • 16.
    Simple REST APIImplementation in PHP REST API objects and their operations • Products – Read (all records) – Read one – Create – Update – Delete – Search • Categories – Read (all records) – Other operations has to be implemented as an exercise
  • 17.
    Web root Foldersand Files Structure
  • 18.
    MySQL Database Import MySQLdatabase using api_db.sql dump file
  • 19.
    Reading all Products UsingPostMan as the client to read all products
  • 20.
    Reading one Product UsingPostMan as the client to read one product
  • 21.
    Creating a Product UsingPostMan as the client to create a product
  • 22.
    Updating a Product UsingPostMan as the client to update a product
  • 23.
    Deleting a Product UsingPostMan as the client to delete a product
  • 24.
    Searching a Product UsingPostMan as the client to search a product
  • 25.