-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwalkscore.php
More file actions
92 lines (77 loc) · 2.27 KB
/
walkscore.php
File metadata and controls
92 lines (77 loc) · 2.27 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
header('Access-Control-Allow-Origin: *');
// The address of the place you want to get a walkscore for. This can be
// anything that you would normally search for in the search box on
// walkscore.com
$address = $_GET['addr'] ?? NULL;
if (!$address) {
respond('Missing Address');
}
// Massage address so I can use it with the walkscore website
$address = str_replace('#', '', $address);
$address = str_replace(' ', '-', $address);
// Attempt to get a response from walkscore. Try up to 10 times before giving
// up, sometimes it fails a couple times.
$url = "https://www.walkscore.com/score/{$address}";
for ($i = 0; $i < 10; $i++) {
$page = @file_get_contents($url);
if ($page) {
break;
}
}
if (!$page) {
respond(
[
err => 'Failed to retrieve Walk Score',
url => $url,
addr => $address,
]
);
}
$matches = [];
// Find the walkscore, transit score, and bike score of the address.
preg_match("/(\d+) Walk Score of /", $page, $matches);
$walkScore = isset($matches[1]) ? intval($matches[1]) : NULL;
preg_match("/(\d+) Transit Score of /", $page, $matches);
$transitScore = isset($matches[1]) ? intval($matches[1]) : NULL;
preg_match("/(\d+) Bike Score of /", $page, $matches);
$bikeScore = isset($matches[1]) ? intval($matches[1]) : NULL;
// Respond with the scores and the walkscore URL that gives the results
respond(
[
'walkScore' => $walkScore,
'transitScore' => $transitScore,
'bikeScore' => $bikeScore,
'url' => $url,
]
);
function respond($data)
{
// The optional variable name to store the results into.
$variableName = $_GET['variable'] ?? NULL;
$isJsonp = isset($variableName);
$code = 200;
// If a string, then assume it is an error
if (is_string($data))
{
$data = [ 'err' => $data ];
$code = 400;
}
else if (array_key_exists('err', $data))
{
$code = 400;
}
// Provide JSONP support
if ($isJsonp)
{
header('Content-Type: application/javascript');
echo "window.{$variableName} = " . json_encode($data) . ";";
}
else
{
header('Content-Type: application/json');
echo json_encode($data);
}
http_response_code($code);
die();
}