Codecademy Logo

APIs

Query Strings

A query string is a part of a URL that’s used to add data to a request.

In the URL: https://google.com/search?q=Codecademy
"q=Codecademy" is the query string

Syntax: Query Parameters

Located at the end of a URL, a query string consists of a question mark (?) followed by one or more key-value pairs, known as query items or parameters.

Each query item is separated by an ampersand.

In the URL: https://google.com/search?q=Codecademy&sourceid=chrome
There are two query parameters connected by an ampersand:
1. q=Codecademy (Key = q, value = Codecademy)
2. sourceid=chrome (Key = sourceid, value = chrome)

The json_decode() Function

In PHP, the json_decode() function decodes a JSON string into a native PHP data type, such as an object or array.

// Assuming $response is a JSON string
$php_data_object = json_decode($response);

Syntax: json_decode()

In PHP, the json_decode() function accepts 2 arguments:

  1. A JSON encoded string
  2. A boolean representing the returned type

If the second argument is true, an associative array is returned. If it is false or not provided, then an object is returned.

// To decode to an object
$data_object = json_decode($response);
// or
$data_object_2 = json_decode($response, false);
// Accessing data from the object
echo $data_object->info->version;
// To decode to an associative array
$data_array = json_decode($response, true);
// Accessing data from the associative array
echo $data_array['info']['version'];

Detecting Failed API Requests

In PHP, whenever the file_get_contents() function cannot complete an operation successfully, the function returns false and additional information is printed to the webpage or console.

This false value can be used to detect failed API requests.

$response = file_get_contents("https://code-academy.com");
if ($response === false) {
echo "Something went wrong!";
}

Validating API Responses

In PHP, when attempting to access a property in an API response from the json_decode() function, it is a best practice to validate the APIs response data before accessing specific properties.

This can be done by using a PHP function such as isset() or empty().

/* Assuming an API response value is assigned to $response and can return nothing, an error object, or a phone number property nested inside of a results object */
$data = json_decode($response, true);
// Checks if the response is empty
if (empty($data)){
echo 'Error: No data received';
// Checks if the response has a error value
} elseif (isset($data['error'])) {
// print error message
echo 'Error: ' . $data['error'];
} else {
// print phone number value
echo 'Phone Number: ' . $data['results'][0]['phone'];
}

Customizing API Requests

API requests often include additional information to customize the response.

For example, a request to the OpenWeather API should include the location of the requested weather data.

Application Programming Interface

An Application Programming Interface, or API, is a tool that makes it easier for developers to access software from another application.

Browser APIs

Browser APIs, a type of Web API, are provided by browsers to give developers access to information that the browser can access from users’ computers.

For example, the Web Audio API is a browser API that provides a powerful and versatile system for controlling audio on the Web.

Third-Party APIs

Third-party APIs are apps that provide some type of functionality or information from a third-party, usually a company.

For example, the OpenWeather API is a third-party API that provides in-depth weather information.

API Documentation

All third-party and Web APIs provide documentation for how to use the API.

API Keys

Some third-party APIs require an API key which is a special token that is given to a developer to interact with the API. These API keys are unique and should be kept secret.

API Response Data

When a successful request is made to an API, data is sent back. Most APIs return data in the form of JSON, and it is up to developers to decide how to consume it.

Learn More on Codecademy