Skip to main content
00:00/00:00
Lecture 8 of 89

Request headers add meta data about the request

Download Course (Free)

Course Content

0 / 89 completed
Section 1: API basics what APIs are and how to use them4 videos

Make an API call access an API from PHP

5m

Decode API results reading JSON in PHP

5m

What is an API

8m

Use API data in a web application

3m
Section 2: Introduction how to use the course and software installation3 videos

Install Composer manage third-party packages and autoload class files

2m

Introduction and welcome how to get the most out of the course

5m

Install a package with a web server, PHP, a database server and phpMyAdmin

6m
Section 3: HTTP basics requests, responses and using cURL8 videos

Request headers add meta data about the request

9mNow Playing

Use cURL instead of file get contents to make an API request

11m

Response codes get the HTTP status code

11m

Get all individual response headers in an array

8m

Response headers read meta data about the response

9m

Use an API that requires a specific request header

8m

Request method change the method to get a different result with the same URL

10m

Request body add a payload to send data along with the request

8m
Section 4: REST and RESTful APIs using them from PHP4 videos

REST and RESTful APIs what are they

7m

Access a RESTful API in PHP with cURL

10m

Use the Guzzle HTTP client for object-oriented API code

11m

Use an SDK compare the Stripe API to its SDK

12m
Section 5: Create a RESTful API build a framework for serving the API9 videos

The front controller get the resource, ID and the request method

3m

Start writing the API enable URL rewriting

4m

Use a client for API development cURL, Postman or HTTPie

3m

Set the HTTP status code best practices

5m

Add a controller class to decide the response

6m

Use Composer's autoloader to load classes automatically

4m

Always return JSON add a generic exception handler and JSON Content-Type header

5m

Make debugging easier add type declarations and enable strict type checking

4m

Send a 405 status code and Allow header for invalid request methods

4m
Section 6: Create a RESTful API create a database and retrieve data from it10 videos

Create a new database and a database user to access it

3m

Create a table to store resource data

1m

Connect to the database from PHP add a Database class

4m

Create a table data gateway class for the resource table

3m

Configure PDO to prevent numeric values from being converted to strings

3m

Move the database connection data to a separate .env file

4m

Show a list of all records

4m

Convert database booleans to boolean literals in the JSON

2m

Show an individual record

3m

Respond with 404 if the resource with the specified ID is not found

3m
Section 7: Create a RESTful API create, update and delete individual resources8 videos

Insert a record into the database and respond with a 201 status code

5m

Get the data from the request as JSON

10m

Add a generic error handler to output warnings as JSON

4m

Validate the data and respond with a 422 status code if invalid

4m

Conditionally validate the data when updating an existing record

4m

Get the data from the request for updating an existing record

5m

Update the record in the database and return a 200 status code

5m

Delete the record in the database and return a 200 status code

2m
Section 8: API key authentication12 videos

Create a table to store user account data

7m

Add a register page to insert a new user record and generate a new API key

11m

Send the API key with the request query string or request header

4m

Check the API key is present in the request and return 400 if not

3m

Create a table data gateway class for the user table

3m

Authenticate the API key and return a 401 status code if invalid

3m

Refactor the front controller to a bootstrap file and Auth class

9m

Add a foreign key relationship to link task records to user records

1m

Retrieve the ID of the authenticated user when authenticating

2m

Restrict the tasks index endpoint to only show the authenticated user's tasks

2m

Restrict the rest of the task endpoints to the authenticated user's tasks

5m

Cache the database connection to avoid multiple connections in the same request

5m
Section 9: An introduction to authentication using access tokens8 videos

An introduction to authentication using access tokens

4m

Generate an encoded access token containing the user details

4m

Select the user record based on the username in the request

3m

Check the username and password and return a 401 status code if invalid

2m

Create the login script and return 400 if the username and password are missing

3m

Pass the access token to the task API endpoints in the authorization header

10m

Validate the access token and decode its contents

6m

Get the authenticated user data from the access token

6m
Section 10: Authentication using JSON Web Tokens (JWTs)8 videos

An introduction to JSON web tokens (JWTs)

5m

Generate a JWT access token in the login endpoint containing JWT claims

4m

Create a class to encode a payload in a JWT

10m

Pass in the secret key used for hashing as a dependency

4m

Add a method to decode the payload from the JWT

10m

Use a custom exception class to return 401 if the signature is invalid

3m

Don't store sensitive data in the JWT

4m

Authenticate the task endpoints using the JWT

5m
Section 11: Expiring and refreshing access tokens14 videos

Why access tokens need to expire and how to refresh them in a user-friendly way

6m

Add an expiry claim to the access token payload when logging in

3m

Throw a custom exception to not accept the JWT if it has expired

3m

Add a refresh endpoint and validate the refresh token in the request

4m

Issue a refresh token in addition to the access token when logging in

2m

Validate the user in the refresh token using the database

4m

Issue a new access token and refresh token to the authenticated user

5m

Create a table to store a refresh token whitelist

2m

Store the refresh token in the whitelist when issued in the login endpoint

4m

Replace the refresh token in the whitelist when issued in the refresh endpoint

4m

Validate the refresh token is on the whitelist and return a 400 response if not

7m

Add a logout endpoint to remove the an active refresh token from the whitelist

6m

See how a single-page application interacts with the API using access tokens

6m

Add a script to clear out expired refresh tokens from the whitelist

7m
Section 12: Conclusion1 videos

Conclusion & where to go from here

2m