Skip to content

Commit 172451a

Browse files
Merge pull request #7 from apalethorpe/master
Add support for HTTPS and Google Maps API keys
2 parents 08901e8 + f65d687 commit 172451a

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Geolocation.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@
1212
class Geolocation
1313
{
1414
// API URL
15-
const API_URL = 'http://maps.googleapis.com/maps/api/geocode/json';
15+
const API_URL = 'maps.googleapis.com/maps/api/geocode/json';
16+
17+
private $api_key;
18+
private $https;
19+
20+
public function __construct($api_key = null, $https = false)
21+
{
22+
$this->https = $https;
23+
24+
if ($api_key) {
25+
$this->api_key = $api_key;
26+
$this->https = true;
27+
}
28+
}
1629

1730
/**
1831
* Do call
@@ -29,14 +42,18 @@ protected function doCall($parameters = array())
2942
}
3043

3144
// define url
32-
$url = self::API_URL . '?';
45+
$url = ($this->https ? 'https://' : 'http://') . self::API_URL . '?';
3346

3447
// add every parameter to the url
3548
foreach ($parameters as $key => $value) $url .= $key . '=' . urlencode($value) . '&';
3649

3750
// trim last &
3851
$url = trim($url, '&');
3952

53+
if ($this->api_key) {
54+
$url .= '&key=' . $this->api_key;
55+
}
56+
4057
// init curl
4158
$curl = curl_init();
4259

0 commit comments

Comments
 (0)