Skip to content

Commit 91c36b9

Browse files
committed
Add premium endpoint and token authentication support for Geonames provider
- Add optional token parameter to Geonames constructor - Add secure flag to enable https://secure.geonames.net endpoint - Support dynamic base URL (api.geonames.org or secure.geonames.net) - Append token parameter to all API requests when provided - Maintain backward compatibility with existing username-only authentication
1 parent 37261a3 commit 91c36b9

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

src/Provider/Geonames/Geonames.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,38 @@ final class Geonames extends AbstractHttpProvider implements Provider
5252
*/
5353
private $username;
5454

55+
/**
56+
* @var string|null
57+
*/
58+
private $token;
59+
60+
/**
61+
* @var string
62+
*/
63+
private $baseUrl;
64+
5565
/**
5666
* @param ClientInterface $client An HTTP adapter
5767
* @param string $username Username login (Free registration at http://www.geonames.org/login)
68+
* @param string|null $token Optional token for premium accounts
69+
* @param bool $secure Use secure endpoint (https://secure.geonames.net) for premium accounts
5870
*/
59-
public function __construct(ClientInterface $client, string $username)
71+
public function __construct(ClientInterface $client, string $username, ?string $token = null, bool $secure = false)
6072
{
6173
if (empty($username)) {
6274
throw new InvalidCredentials('No username provided.');
6375
}
6476

6577
$this->username = $username;
78+
$this->token = $token;
79+
80+
// Determine base URL based on secure flag
81+
if ($secure) {
82+
$this->baseUrl = 'https://secure.geonames.net';
83+
} else {
84+
$this->baseUrl = 'http://api.geonames.org';
85+
}
86+
6687
parent::__construct($client);
6788
}
6889

@@ -75,7 +96,15 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7596
throw new UnsupportedOperation('The Geonames provider does not support IP addresses.');
7697
}
7798

78-
$url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $query->getLimit(), $this->username);
99+
$url = sprintf(
100+
'%s/searchJSON?q=%s&maxRows=%d&style=full&username=%s',
101+
$this->baseUrl,
102+
urlencode($address),
103+
$query->getLimit(),
104+
$this->username
105+
);
106+
107+
$url = $this->appendToken($url);
79108

80109
return $this->executeQuery($url, $query->getLocale());
81110
}
@@ -86,7 +115,16 @@ public function reverseQuery(ReverseQuery $query): Collection
86115
$longitude = $coordinates->getLongitude();
87116
$latitude = $coordinates->getLatitude();
88117

89-
$url = sprintf(self::REVERSE_ENDPOINT_URL, $latitude, $longitude, $query->getLimit(), $this->username);
118+
$url = sprintf(
119+
'%s/findNearbyPlaceNameJSON?lat=%F&lng=%F&style=full&maxRows=%d&username=%s',
120+
$this->baseUrl,
121+
$latitude,
122+
$longitude,
123+
$query->getLimit(),
124+
$this->username
125+
);
126+
127+
$url = $this->appendToken($url);
90128

91129
return $this->executeQuery($url, $query->getLocale());
92130
}
@@ -96,7 +134,7 @@ public function reverseQuery(ReverseQuery $query): Collection
96134
*/
97135
public function getCountryInfo(?string $country = null, ?string $locale = null): array
98136
{
99-
$url = sprintf(self::BASE_ENDPOINT_URL, 'countryInfoJSON', $this->username);
137+
$url = sprintf('%s/countryInfoJSON?username=%s', $this->baseUrl, $this->username);
100138

101139
if (isset($country)) {
102140
$url = sprintf('%s&country=%s', $url, $country);
@@ -109,6 +147,8 @@ public function getCountryInfo(?string $country = null, ?string $locale = null):
109147
$url = sprintf('%s&lang=%s', $url, substr($locale, 0, 2));
110148
}
111149

150+
$url = $this->appendToken($url);
151+
112152
$content = $this->getUrlContents($url);
113153
if (null === $json = json_decode($content)) {
114154
throw InvalidServerResponse::create($url);
@@ -217,4 +257,16 @@ private function executeQuery(string $url, ?string $locale = null): AddressColle
217257

218258
return new AddressCollection($results);
219259
}
260+
261+
/**
262+
* Append token parameter to URL if token is provided.
263+
*/
264+
private function appendToken(string $url): string
265+
{
266+
if (null !== $this->token && '' !== $this->token) {
267+
$url = sprintf('%s&token=%s', $url, $this->token);
268+
}
269+
270+
return $url;
271+
}
220272
}

0 commit comments

Comments
 (0)