Police.uk API

So, updated post below now;

On Friday I received my api keys for the new police.uk website, first thoughts; poor documentation and not brilliant functionally, but it works! They have had to move the api to a new location due to overwhelming demand on the site. After reading through it, I decided to try and pull back the neighborhood crime rates based on location.

After looking through the documentation I decided to do a two step call, first, the the longitude and latitude are entered into a simple form, this is crime1.php;

  
<form action="crime.php" method="post">
    Please Enter latitude;
<input name="lat" type="text" />
    Please Enter Long;
<input name="long" type="text" />
<input type="submit" />
</form>

Nice and simple, a quick form where you can enter the latitude and longitude.

Next, crime.php, there are four steps to this. First one is to send the lat/long details to police.uk to get the name of the force and the neighborhood code for that area, this is using cURL to send the HTTP Get request, receiving the response in JSON;

  
<!--?php<br /-->$lat = ($_POST['lat']);
$long = ($_POST['long']);
$ch = curl_init();
//pull force data
curl_setopt($ch, CURLOPT_URL, "http://policeapi2.rkh.co.uk/api/locate-neighbourhood?q="; . $lat . "," . $long. "");  
curl_setopt($ch, CURLOPT_USERPWD, "user:password");  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$result = curl_exec($ch);
curl_close($ch);  
$test = json_decode($result,true);

The $long and $lat is pulling the details from our previous page, crime1.php.To adapt this script you'll need to sign
up to police.uk to get an api key, once you have that just replace the "user:password" with your details. The $test part is just pulls the JSON response and puts it into a PHP variable.

The second part is to pull out and the force information;

  
$force = $test["force"];
$area = $test["neighbourhood"];

The "force" and "neighbourhood" parts are just the name within the returned array.

The third part;

  
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "http://policeapi2.rkh.co.uk/api/"; . $force . "/" . $area. "/crime");  
curl_setopt($ch1, CURLOPT_USERPWD, "dosan29:8bf3d5c9d17d3bb5cc9c76fbcb1c94df");  
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);  
$crimeresult = curl_exec($ch1);
curl_close($ch1);  
$crimetest = json_decode($crimeresult,true);
$aParts   = explode(" ", $crimeresult);
$anti =$aParts["32"];
$antitotal=$aParts["86"];
$other =$aParts["111"];
$othertotal =$aParts["165"];
$rob =$aParts["269"];
$robtotal =$aParts["323"];
$bug =$aParts["348"];
$bugtotal =$aParts["402"];
$car =$aParts["427"];
$cartotal =$aParts["481"];
$fight =$aParts["506"];
$fighttotal =$aParts["560"];

This is pulling the $force and $area and, using the same as the first step, sending off a request via cURL to police.uk to request the crime rate details, then I have used the explode function, which returns and array of strings from the request, this then gives each part of the JSON response a number we can pull (the response is really naff and not very well presented). I have then named and pulled each part I want from the response, so in this script the type of crime, and the total amount of those crimes in this area.

Now, the last part;

  
print "The crime rate stats for ($lat),($long) are..";  
print_r ($anti);  
print_r ($antitotal);  
echo "";  
print_r ($other);  
print_r ($othertotal);  
echo "";  
print_r ($rob);  
print_r ($robtotal);  
echo "";  
print_r ($bug);  
print_r ($bugtotal);  
echo "";  
print_r ($car);  
print_r ($cartotal);  
echo "";  
print_r ($fight);  
print_r ($fighttotal);  
echo "

It Works!";  

This is just simply printing the details, which have been named in section three. I need to clean this up a bit, but don't really have time at the moment!

The complete code can be found at the bottom.

Next steps?

Next I was going to add another step, post code to long/lat, which would make it more user friendly, but thanks to the lovely laws in the UK, post code to long/lat isn't possible without paying the royal mail a silly amount of money...something which I need to research more and see I can petition the Government to opensource this. I may also try to see if I can locate long/lat from the IP address, thus meaning the user doesn't even have to add any details in! Check back at the end of the week....I may have completed it by then!

Complete code;

  
<!--?php<br /-->$lat = ($_POST['lat']);
$long = ($_POST['long']);
$ch = curl_init();
//pull force data
curl_setopt($ch, CURLOPT_URL, "http://policeapi2.rkh.co.uk/api/locate-neighbourhood?q="; . $lat . "," . $long. "");  
curl_setopt($ch, CURLOPT_USERPWD, "usr:pwd");  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
$result = curl_exec($ch);
curl_close($ch);  
$test = json_decode($result,true);

//name force data
$force = $test["force"];
$area = $test["neighbourhood"];

//get crime ratesdata
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, "http://policeapi2.rkh.co.uk/api/"; . $force . "/" . $area. "/crime");  
curl_setopt($ch1, CURLOPT_USERPWD, "dosan29:8bf3d5c9d17d3bb5cc9c76fbcb1c94df");  
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);  
$crimeresult = curl_exec($ch1);
curl_close($ch1);  
$crimetest = json_decode($crimeresult,true);
$aParts   = explode(" ", $crimeresult);
$anti =$aParts["32"];
$antitotal=$aParts["86"];
$other =$aParts["111"];
$othertotal =$aParts["165"];
$rob =$aParts["269"];
$robtotal =$aParts["323"];
$bug =$aParts["348"];
$bugtotal =$aParts["402"];
$car =$aParts["427"];
$cartotal =$aParts["481"];
$fight =$aParts["506"];
$fighttotal =$aParts["560"];

// $aParts[0] is now 'Hello'
// $aParts[1] is now 'World'

//ouput crime type and data
print "The crime rate stats for ($lat),($long) are..";  
print_r ($anti);  
print_r ($antitotal);  
echo "";  
print_r ($other);  
print_r ($othertotal);  
echo "";  
print_r ($rob);  
print_r ($robtotal);  
echo "";  
print_r ($bug);  
print_r ($bugtotal);  
echo "";  
print_r ($car);  
print_r ($cartotal);  
echo "";  
print_r ($fight);  
print_r ($fighttotal);  
echo "It Works!";  
?>

Give it a go here;

richardallen.co.uk/mashups/crime1.php

Show Comments