11<?php
22
33namespace JeroenDesloovere \Geolocation ;
4+
45use JeroenDesloovere \Geolocation \Result \Address ;
56use JeroenDesloovere \Geolocation \Result \Coordinates ;
67
1415class Geolocation
1516{
1617 // 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+ }
1832
1933 /**
2034 * Do call
2135 *
2236 * @param array $parameters
23- * @return object
37+ * @return mixed
2438 * @throws Exception
2539 */
2640 protected function doCall ($ parameters = array ())
@@ -31,14 +45,18 @@ protected function doCall($parameters = array())
3145 }
3246
3347 // define url
34- $ url = self ::API_URL . '? ' ;
48+ $ url = ( $ this -> https ? ' https:// ' : ' http:// ' ) . self ::API_URL . '? ' ;
3549
3650 // add every parameter to the url
3751 foreach ($ parameters as $ key => $ value ) $ url .= $ key . '= ' . urlencode ($ value ) . '& ' ;
3852
3953 // trim last &
4054 $ url = trim ($ url , '& ' );
4155
56+ if ($ this ->api_key ) {
57+ $ url .= '&key= ' . $ this ->api_key ;
58+ }
59+
4260 // init curl
4361 $ curl = curl_init ();
4462
@@ -64,8 +82,9 @@ protected function doCall($parameters = array())
6482 // redefine response as json decoded
6583 $ response = json_decode ($ response );
6684
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 );
6988 }
7089
7190 // return the content
0 commit comments