Created
June 7, 2014 01:34
-
-
Save psaia/cd68dbdc29f7299963f9 to your computer and use it in GitHub Desktop.
crimes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ERROR | E_PARSE); | |
header('Content-Type: application/json'); | |
class API { | |
const CACHE_FILE = "./addresses.txt"; | |
private $hash = array(); | |
public $crimes = array(); | |
public function __construct() { | |
if (!file_exists(self::CACHE_FILE)) { | |
if (!file_put_contents(self::CACHE_FILE, '')) { | |
throw new Exception('Could not create cache file!'); | |
} | |
} | |
if (!is_writable(self::CACHE_FILE)) { | |
throw new Exception( 'Cache file is not writable.'); | |
} | |
$this->hash = $this->storage_hash(); | |
} | |
// Make sure the server this is requesting from is authorized by google. | |
// https://console.developers.google.com | |
private function lookup($address) { | |
$string = str_replace (" ", "+", urlencode($address)); | |
$response = json_decode($this->req("https://maps.googleapis.com/maps/api/geocode/json?address=".$string), true); | |
if ($response['status'] != 'OK') { | |
return null; | |
} | |
$geometry = $response['results'][0]['geometry']; | |
$longitude = $geometry['location']['lat']; | |
$latitude = $geometry['location']['lng']; | |
$array = array( | |
'latitude' => $geometry['location']['lng'], | |
'longitude' => $geometry['location']['lat'], | |
'location_type' => $geometry['location_type'], | |
); | |
return $array; | |
} | |
private function storage_hash() { | |
if ($contents = file_get_contents(self::CACHE_FILE)) { | |
if (!empty($contents) && $hash = unserialize($contents)) { | |
// var_dump($hash); | |
return $hash; | |
} | |
} | |
return array(); | |
} | |
private function to_cache($address, $coords) { | |
$this->hash[$address] = $coords; | |
if (!file_put_contents(self::CACHE_FILE, serialize($this->hash))) { | |
throw new Exception("Could not wriite to cache."); | |
} | |
} | |
private function req($url) { | |
$curl = curl_init(); | |
curl_setopt_array($curl, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $url, | |
CURLOPT_USERAGENT => "Lev Interactive Experiment" | |
)); | |
$res = curl_exec($curl); | |
curl_close($curl); | |
return $res; | |
} | |
public static function response($arr) { | |
if (isset($arr['error'])) { | |
http_response_code(400); | |
} | |
return json_encode($arr, JSON_PRETTY_PRINT); | |
} | |
public function load() { | |
$this->crimes = array(); | |
$xml = simplexml_load_string( | |
$this->req("http://data.octo.dc.gov/feeds/crime_incidents/crime_incidents_current.xml"), | |
null, | |
LIBXML_NOCDATA | |
); | |
if (!$xml) { | |
throw new Exception("Sorry, data.octo.dc.gov must be down."); | |
} | |
foreach($xml->entry as $entry) { | |
$data = $entry->content->children('dcst', true)->ReportedCrime->children('dcst', true); | |
$data = json_decode(json_encode($data)); | |
if (isset($this->hash[$data->blocksiteaddress])) { | |
$data->coords = $this->hash[$data->blocksiteaddress]; | |
} else { | |
$data->coords = $this->lookup($data->blocksiteaddress); | |
$this->to_cache($data->blocksiteaddress, $data->coords); | |
} | |
$this->crimes[] = $data; | |
} | |
} | |
} | |
try { | |
$api = new API(); | |
$api->load(); | |
echo API::response($api->crimes); | |
} catch(Exception $e) { | |
echo API::response($e->getMessage()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment