-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
54 lines (46 loc) · 1.23 KB
/
process.php
File metadata and controls
54 lines (46 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/php
<?php
require 'postcodes.php';
require 'config.php';
declare(ticks = 1);
pcntl_signal(SIGINT, function() {
global $results;
echo "\n\n";
echo json_encode($results, JSON_PRETTY_PRINT);
echo "\n\n";
die;
});
$results = [];
$api_key = 0;
$i = 0;
while ($i < count($postcodes)) {
$key = $google_api_keys[$api_key];
plog("Using key: {$key}");
plog("Grabbing lat/long for {$postcodes[$i]}");
$url = 'https://maps.googleapis.com/maps/api/geocode/json?key=' .
$key . '&address=' . urlencode($postcodes[$i]);
$data = file_get_contents($url);
$data = json_decode($data);
if ($data->status == 'OVER_QUERY_LIMIT') {
if ($api_key == count($google_api_keys)) {
plog("Out of API keys");
$i = count($postcodes);
}
$api_key++;
} else {
$latlng = $data->results[0]->geometry->location;
array_push($results, [
'postcode' => $postcodes[$i],
'lat' => $latlng->lat,
'lon' => $latlng->lng
]);
$i++;
}
}
echo "\n\n";
echo json_encode($results, JSON_PRETTY_PRINT);
echo "\n\n";
function plog($msg) {
$date = date('H:i:s');
echo "[{$date}] {$msg}\n";
}