1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Suilven\UKPostCodes; |
4
|
|
|
|
5
|
|
|
//Based on code by Ryan Hart 2016 https://github.com/ryryan/Postcodes-IO-PHP/blob/master/Postcodes-IO-PHP.php |
6
|
|
|
|
7
|
|
|
use Suilven\UKPostCodes\Models\PostCode; |
8
|
|
|
|
9
|
|
|
class API |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param string $postcode |
13
|
|
|
* @return false|PostCode |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
public function lookup($postcode) |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$jsonurl = "https://api.postcodes.io/postcodes/".$postcode; |
18
|
|
|
$json = $this->request($jsonurl); |
19
|
|
|
$decoded = json_decode($json, true); |
20
|
|
|
if ($decoded['status'] == 200) { |
21
|
|
|
return new PostCode($decoded['result']); |
22
|
|
|
} else { |
23
|
|
|
return false; |
24
|
|
|
} |
25
|
|
|
return false; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
View Code Duplication |
public function bulkLookup($postcodes) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$data_string = json_encode(array('postcodes' => $postcodes)); |
32
|
|
|
$ch = curl_init('https://api.postcodes.io/postcodes'); |
33
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
34
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); |
35
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
36
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
37
|
|
|
'Content-Type: application/json', |
38
|
|
|
'Content-Length: ' . strlen($data_string))); |
39
|
|
|
|
40
|
|
|
$result = curl_exec($ch); |
41
|
|
|
curl_close($ch); |
42
|
|
|
$decoded = json_decode($result); |
43
|
|
|
if ($decoded->status == 200) { |
44
|
|
|
return $decoded->result; |
45
|
|
|
} else { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
return false; |
|
|
|
|
49
|
|
|
} |
50
|
|
View Code Duplication |
public function nearestPostcodesFromLongLat($longitude, $latitude) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$jsonurl = "https://api.postcodes.io/postcodes?lon=".$longitude."&lat=".$latitude; |
53
|
|
|
$json = $this->request($jsonurl); |
54
|
|
|
|
55
|
|
|
$decoded = json_decode($json); |
56
|
|
|
if ($decoded->status == 200) { |
57
|
|
|
return $decoded->result; |
58
|
|
|
} else { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
return false; |
|
|
|
|
62
|
|
|
} |
63
|
|
View Code Duplication |
public function bulkReverseGeocoding($geolocations) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$data_string = json_encode(array('geolocations' => $geolocations)); |
66
|
|
|
|
67
|
|
|
$ch = curl_init('https://api.postcodes.io/postcodes'); |
68
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
69
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); |
70
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
71
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
72
|
|
|
'Content-Type: application/json', |
73
|
|
|
'Content-Length: ' . strlen($data_string))); |
74
|
|
|
|
75
|
|
|
$result = curl_exec($ch); |
76
|
|
|
curl_close($ch); |
77
|
|
|
$decoded = json_decode($result); |
78
|
|
|
if ($decoded->status == 200) { |
79
|
|
|
return $decoded->result; |
80
|
|
|
} else { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
return false; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool|PostCode a random PostCode |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function random() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$jsonurl = "https://api.postcodes.io/random/postcodes/"; |
93
|
|
|
$json = $this->request($jsonurl); |
94
|
|
|
|
95
|
|
|
$decoded = json_decode($json, true); |
96
|
|
|
if ($decoded['status'] == 200) { |
97
|
|
|
return new PostCode($decoded['result']); |
98
|
|
|
} else { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
return false; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
public function validate($postcode) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$jsonurl = "https://api.postcodes.io/postcodes/".$postcode."/validate"; |
108
|
|
|
$json = $this->request($jsonurl); |
109
|
|
|
|
110
|
|
|
$decoded = json_decode($json); |
111
|
|
|
if ($decoded->status == 200) { |
112
|
|
|
if ($decoded->result == 1) { |
|
|
|
|
113
|
|
|
return true; |
114
|
|
|
} else { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
return false; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
View Code Duplication |
public function nearest($postcode) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$jsonurl = "https://api.postcodes.io/postcodes/".$postcode."/nearest"; |
127
|
|
|
$json = $this->request($jsonurl); |
128
|
|
|
|
129
|
|
|
$decoded = json_decode($json); |
130
|
|
|
if ($decoded->status == 200) { |
131
|
|
|
return $decoded->result; |
132
|
|
|
} else { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
return false; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
View Code Duplication |
public function partial($postcode) |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$jsonurl = "https://api.postcodes.io/postcodes/".$postcode."/autocomplete"; |
142
|
|
|
$json = $this->request($jsonurl); |
143
|
|
|
|
144
|
|
|
$decoded = json_decode($json); |
145
|
|
|
if ($decoded->status == 200) { |
146
|
|
|
return $decoded->result; |
147
|
|
|
} else { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
return false; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
public function query($postcode) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$jsonurl = "https://api.postcodes.io/postcodes?q=".$postcode; |
157
|
|
|
$json = $this->request($jsonurl); |
158
|
|
|
|
159
|
|
|
$decoded = json_decode($json); |
160
|
|
|
if ($decoded->status == 200) { |
161
|
|
|
return $decoded->result; |
162
|
|
|
} else { |
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
return false; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
public function lookupTerminated($postcode) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$jsonurl = "https://api.postcodes.io/terminated_postcodes/".$postcode; |
172
|
|
|
$json = $this->request($jsonurl); |
173
|
|
|
|
174
|
|
|
$decoded = json_decode($json); |
175
|
|
|
if ($decoded->status == 200) { |
176
|
|
|
return $decoded->result; |
177
|
|
|
} else { |
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
return false; |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
public function lookupOutwardCode($code) |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
$jsonurl = "https://api.postcodes.io/outcodes/".$code; |
187
|
|
|
$json = $this->request($jsonurl); |
188
|
|
|
|
189
|
|
|
$decoded = json_decode($json); |
190
|
|
|
if ($decoded->status == 200) { |
191
|
|
|
return $decoded->result; |
192
|
|
|
} else { |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
return false; |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
View Code Duplication |
public function nearestOutwardCode($code) |
|
|
|
|
200
|
|
|
{ |
201
|
|
|
$jsonurl = "https://api.postcodes.io/outcodes/".$code."/nearest"; |
202
|
|
|
$json = $this->request($jsonurl); |
203
|
|
|
|
204
|
|
|
$decoded = json_decode($json); |
205
|
|
|
if ($decoded->status == 200) { |
206
|
|
|
return $decoded->result; |
207
|
|
|
} else { |
208
|
|
|
return false; |
209
|
|
|
} |
210
|
|
|
return false; |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
|
214
|
|
View Code Duplication |
public function nearestOutwardCodeFromLongLat($longitude, $latitude) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$jsonurl = "https://api.postcodes.io/outcodes?lon=".$longitude."&lat=".$latitude; |
217
|
|
|
$json = $this->request($jsonurl); |
218
|
|
|
|
219
|
|
|
$decoded = json_decode($json); |
220
|
|
|
if ($decoded->status == 200) { |
221
|
|
|
return $decoded->result; |
222
|
|
|
} else { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
return false; |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
public function distance($postcode1, $postcode2, $unit) |
230
|
|
|
{ |
231
|
|
|
//adapted from http://www.geodatasource.com/developers/php |
232
|
|
|
/* |
233
|
|
|
Units: |
234
|
|
|
M = Miles |
235
|
|
|
N = Nautical Miles |
236
|
|
|
K = Kilometers |
237
|
|
|
*/ |
238
|
|
|
$postcode1 = $this->lookup($postcode1); |
239
|
|
|
$postcode2 = $this->lookup($postcode2); |
240
|
|
|
|
241
|
|
|
if ($postcode1 == null || $postcode2 == null) { |
242
|
|
|
return false; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$theta = $postcode1->longitude - $postcode2->longitude; |
246
|
|
|
$dist = sin(deg2rad($postcode1->latitude)) * sin(deg2rad($postcode2->latitude)) + |
247
|
|
|
cos(deg2rad($postcode1->latitude)) * cos(deg2rad($postcode2->latitude)) * cos(deg2rad($theta)); |
248
|
|
|
$dist = acos($dist); |
249
|
|
|
$dist = rad2deg($dist); |
250
|
|
|
$miles = $dist * 60 * 1.1515; |
251
|
|
|
$unit = strtoupper($unit); |
252
|
|
|
|
253
|
|
|
if ($unit == "K") { |
254
|
|
|
return ($miles * 1.609344); |
255
|
|
|
} elseif ($unit == "N") { |
256
|
|
|
return ($miles * 0.8684); |
257
|
|
|
} else { |
258
|
|
|
return $miles; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Execute a request |
264
|
|
|
* |
265
|
|
|
* @param string $jsonurl |
266
|
|
|
* @return bool|string |
267
|
|
|
*/ |
268
|
|
|
public function request($jsonurl) |
269
|
|
|
{ |
270
|
|
|
$ch = curl_init(); |
271
|
|
|
curl_setopt($ch, CURLOPT_URL, str_replace(' ', '%20', $jsonurl)); |
272
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); |
273
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
274
|
|
|
'Content-Type: application/json', |
275
|
|
|
]); |
276
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
277
|
|
|
$response = curl_exec($ch); |
278
|
|
|
curl_close($ch); |
279
|
|
|
return $response; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.