Skip to content

Commit 87b69d6

Browse files
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/Geolocation.php
2 parents e0ddb7c + 20655c7 commit 87b69d6

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/Exception.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,4 @@ public static function noCoordinatesFoundforAddress(array $addressData): Excepti
2626
With address: "' . implode(', ', $addressData) . '".'
2727
);
2828
}
29-
30-
public static function overQueryLimit(): Exception
31-
{
32-
return new self(
33-
'You have exceeded your daily request quota for this API.
34-
We recommend registering for a key at the Google Developers Console:
35-
https://console.developers.google.com/apis/credentials?project=_'
36-
);
37-
}
3829
}

src/Geolocation.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace JeroenDesloovere\Geolocation;
4+
45
use JeroenDesloovere\Geolocation\Result\Address;
56
use JeroenDesloovere\Geolocation\Result\Coordinates;
67

@@ -14,13 +15,26 @@
1415
class 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

Comments
 (0)