$request
variable.
$request = 'https://data.police.uk/api/forces';
We can then take the result and again use the file_get_contents()
function within PHP to read the entire response into a string and then decode the JSON formatted string into an array and print that array. This call returns both a "id" and "name" attribute. This "id" attribute serves as the primary key for the following service requests as well. Let's print the name attribute to see a list of all police forces in England, Wales, and Northern Ireland.
$request = 'https://data.police.uk/api/forces/metropolitan';
As you can see, the request requires the user to append a force specific "id" attribute to the end of the request URL. You may examine the information returned from using this API request regarding the Metropolitan Police Force here.
One of the more interesting features of this API is the ability to retrieve substantial force specific information, including information about Senior Officers within a specific police force. Let's take a look at the senior officers of the Gloucestershire police force. First, we form our Force Senior Officers request (once again appending the force specific "id" to the end of the URL in the form of ...forces/["id"]/people
) and place a call to the API as so.
$request = 'https://data.police.uk/api/forces/gloucestershire/people';
Then we need to read the response into a string and then decode the JSON formatted string.
$response = file_get_contents($request);
$JSONarray = json_decode($response, true);
Now all that is left is to print the key:value pairs of the array. We can do that with the following:
foreach ($JSONarray as $key => $value) {
echo $value["name"] . "<"br">";
echo $value["rank"] . "<"br">";
echo $value["bio"] . "<"br">";
}
Which results in the name, rank, and bio of the senior officers of the Gloucestershire police force. Unfortunately, this API does not contain this information for the majority of police forces, only a few have provided as many details as this particular police department. In addition to Force specific information, the API also provides access to Crime specific information as well.