Skip to content

Latest commit

 

History

History
34 lines (34 loc) · 1.83 KB

File metadata and controls

34 lines (34 loc) · 1.83 KB

API Tutorial in PHP

  1. API Basics
  2. Request and Response using cURL - (api.weatherapi.com)
    1. curl_setopt_array()
    2. curl_exec()
    3. curl_getinfo()
  3. REST and REST API using cURL (api.github.com)
    1. Guzzle (external library) - install using composer
    2. Stripe SDK (external library provided by Stripe) - install using composer
  4. Build a framework to access API
    1. URL Rewriting using .htaccess file
    2. Simple Routing using $_SERVER superglobal variable
    3. Creating Controller class - (TaskController) to process reqeuests.
    4. HTTP status codes - best practices
    5. Catch errors using default exception handler
    6. Return JSON response using json_encode() and json_decode()
  5. Retrieve Data from Database in JSON (using PDO)
    1. Create a Separate class for accessing the database using PDO
    2. Injecting the instance of the class into the Controller to retrieve data from database
    3. Storing config data into .env file - install "vlucas/dotenv" using composer
  6. Create RESTFul APIs to perform CRUD operations
    1. Generic ErrorHandler class to return error in json
    2. Insert a Record and return 201 status code
    3. Validate data and return 422 status code for invalid parameters
    4. Delete Record and return 200 status code.
    5. Return 405 status code, if the request is not among GET, POST,PATCH, PUT, DELETE
    6. Return 404 status code, if the resource is not found
  7. Create API authentication
    1. Create a User with plain register page and display their API key.
    2. Generate API key which is randomly generated 32bit characters with randombytes() function and bin2hex() function.
    3. Store the generated API key in the database for authentication
    4. Bootstrap the code to remove repeated code.
    5. Create Auth Class for authentication
    6. Restrict the endpoints based on the user's API Key.