1
1
<?php
2
2
3
3
namespace JeroenDesloovere \Geolocation ;
4
+
4
5
use JeroenDesloovere \Geolocation \Result \Address ;
5
6
use JeroenDesloovere \Geolocation \Result \Coordinates ;
6
7
14
15
class Geolocation
15
16
{
16
17
// API URL
17
- const API_URL = 'http://maps.googleapis.com/maps/api/geocode/json ' ;
18
+ const API_URL = 'maps.googleapis.com/maps/api/geocode/json ' ;
19
+
20
+ private $ api_key ;
21
+ private $ https ;
22
+
23
+ public function __construct ($ api_key = null , $ https = false )
24
+ {
25
+ $ this ->https = $ https ;
26
+
27
+ if ($ api_key ) {
28
+ $ this ->api_key = $ api_key ;
29
+ $ this ->https = true ;
30
+ }
31
+ }
18
32
19
33
/**
20
34
* Do call
21
35
*
22
36
* @param array $parameters
23
- * @return object
37
+ * @return mixed
24
38
* @throws Exception
25
39
*/
26
40
protected function doCall ($ parameters = array ())
@@ -31,14 +45,18 @@ protected function doCall($parameters = array())
31
45
}
32
46
33
47
// define url
34
- $ url = self ::API_URL . '? ' ;
48
+ $ url = ( $ this -> https ? ' https:// ' : ' http:// ' ) . self ::API_URL . '? ' ;
35
49
36
50
// add every parameter to the url
37
51
foreach ($ parameters as $ key => $ value ) $ url .= $ key . '= ' . urlencode ($ value ) . '& ' ;
38
52
39
53
// trim last &
40
54
$ url = trim ($ url , '& ' );
41
55
56
+ if ($ this ->api_key ) {
57
+ $ url .= '&key= ' . $ this ->api_key ;
58
+ }
59
+
42
60
// init curl
43
61
$ curl = curl_init ();
44
62
@@ -64,8 +82,9 @@ protected function doCall($parameters = array())
64
82
// redefine response as json decoded
65
83
$ response = json_decode ($ response );
66
84
67
- if ($ response ->status === 'OVER_QUERY_LIMIT ' ) {
68
- throw Exception::overQueryLimit ();
85
+ // API returns with an error
86
+ if (isset ($ response ->error_message )) {
87
+ throw new Exception ($ response ->error_message );
69
88
}
70
89
71
90
// return the content
0 commit comments