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
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=chromeThere are two query parameters connected by an ampersand:1. q=Codecademy (Key = q, value = Codecademy)2. sourceid=chrome (Key = sourceid, value = chrome)
json_decode()
FunctionIn 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);
json_decode()
In PHP, the json_decode()
function accepts 2 arguments:
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 objectecho $data_object->info->version;// To decode to an associative array$data_array = json_decode($response, true);// Accessing data from the associative arrayecho $data_array['info']['version'];
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!";}
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 emptyif (empty($data)){echo 'Error: No data received';// Checks if the response has a error value} elseif (isset($data['error'])) {// print error messageecho 'Error: ' . $data['error'];} else {// print phone number valueecho 'Phone Number: ' . $data['results'][0]['phone'];}
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.
An Application Programming Interface, or API, is a tool that makes it easier for developers to access software from another application.
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 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.
All third-party and Web APIs provide documentation for how to use the API.
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.
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.