-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPostcodeAPI.php
120 lines (98 loc) · 2.74 KB
/
PostcodeAPI.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
class PostcodeAPI
{
/*
* Get your API key at: http://www.postcodeapi.nu/
* API Docs: http://api.postcodeapi.nu/docs/
*/
private $API_URL = 'http://api.postcodeapi.nu/';
private $API_KEY = 'GET_YOUR_OWN';
/**
* Setup API with key
* @param string $api_key PostcodeAPI.nu api-key
*/
public function __construct($api_key)
{
$this->API_KEY = $api_key;
}
/**
* Get information about zipcode
* @param string $pc zipcode
* @return object object with
*/
public function getInfo( $pc )
{
return $this->request( $pc );
}
/**
* Get distance between two zipcodes.
* @param string $pc1 Start zipcode
* @param string $pc2 End zipcode
* @return float Distance in meters
*/
public function getDistance( $pc1, $pc2 )
{
$start = $this->request( $pc1 );
$end = $this->request( $pc2 );
if( false === $start ) return false;
if( false === $end ) return false;
return $this->getCoordDistance( $start->latitude, $end->latitude, $start->longitude, $end->longitude );
}
/**
* Get distance between two coordinates using the haversine formula.
* @param float $lat1 Start latitude
* @param float $lat2 End latitude
* @param float $lon1 Start longitude
* @param float $lon2 End longitude
* @return float Distance in meters
*/
public function getCoordDistance( $lat1, $lat2, $lon1, $lon2 )
{
$R = 6371; // mean earth readius (km)
$dLat = deg2rad( $lat2-$lat1 );
$dLon = deg2rad( $lon2-$lon1 );
$lat1 = deg2rad( $lat1 );
$lat2 = deg2rad( $lat2 );
$a = sin($dLat/2) * sin($dLat/2) + sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);
$c = 2 * atan2( sqrt($a) , sqrt(1-$a) );
$distance = $R * $c;
return round($distance * 1000);
}
/**
* API request
* @param string $url URL to load
* @return object JSON response converted to object
*/
private function request( $pc )
{
$params = array();
$pc_len = strlen( $pc );
switch( $pc_len ){
case 5:
$params[] = 'type=p5';
break;
case 4:
$params[] = 'type=p4';
break;
default:
$params[] = 'type=p6';
break;
}
$url = $this->API_URL . $pc . '?' . implode('&', $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Api-Key: ' . $this->API_KEY
));
$result = curl_exec( $ch );
curl_close( $ch );
$json = json_decode( $result );
if( $json->success === true ){
return $json->resource;
} else {
return false;
}
}
}