JSON APIs

A JSON API allows a web application to exchange data with a server without reloading the entire page. For example, a PHP script can query a MySQL database, retrieve records, and return them as JSON instead of generating HTML. JavaScript in the browser can then use fetch() to request that data, convert the JSON into JavaScript objects, and update the DOM. The same API can also accept requests to add, modify, or delete records in the database.

PHP can also act as a client of a JSON API, requesting data from another server rather than providing it. A PHP script can make an HTTP request, receive a JSON response, and use json_decode() to convert that data into PHP arrays or objects. We're going to use PHP to retrieve a random person's information and a random music album from two different JSON APIs, combine that information, and store it in your own MySQL database.

This all happens on the server, without requiring JavaScript in the browser.

Preperation

In class: 

  • Let's get going on the Homework.

Check Your Understanding

  • Why might you need an API key to use a third party API service?