Skip to content

Commit 87805ef

Browse files
#8: Destructor should check curl resource before closing
- Cast floats to string before rawurlencode
1 parent 4001d51 commit 87805ef

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

src/PostcodeNl/Api/Client.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Client
3232
/** @var string A platform identifier, a short description of the platform using the API client. */
3333
protected $_platform;
3434
/** @var resource */
35-
protected $_curlHandler;
35+
protected $_curlHandle;
3636
/** @var array Response headers received in the most recent API call. */
3737
protected $_mostRecentResponseHeaders = [];
3838

@@ -54,18 +54,18 @@ public function __construct(string $key, string $secret, string $platform)
5454
throw new CurlNotLoadedException('Cannot use Postcode.nl International Autocomplete client, the server needs to have the PHP `cURL` extension installed.');
5555
}
5656

57-
$this->_curlHandler = curl_init();
58-
curl_setopt($this->_curlHandler, CURLOPT_CUSTOMREQUEST, 'GET');
59-
curl_setopt($this->_curlHandler, CURLOPT_RETURNTRANSFER, true);
60-
curl_setopt($this->_curlHandler, CURLOPT_CONNECTTIMEOUT, 2);
61-
curl_setopt($this->_curlHandler, CURLOPT_TIMEOUT, 5);
62-
curl_setopt($this->_curlHandler, CURLOPT_USERAGENT, $this->_getUserAgent());
57+
$this->_curlHandle = curl_init();
58+
curl_setopt($this->_curlHandle, CURLOPT_CUSTOMREQUEST, 'GET');
59+
curl_setopt($this->_curlHandle, CURLOPT_RETURNTRANSFER, true);
60+
curl_setopt($this->_curlHandle, CURLOPT_CONNECTTIMEOUT, 2);
61+
curl_setopt($this->_curlHandle, CURLOPT_TIMEOUT, 5);
62+
curl_setopt($this->_curlHandle, CURLOPT_USERAGENT, $this->_getUserAgent());
6363

6464
if (isset($_SERVER['HTTP_REFERER']))
6565
{
66-
curl_setopt($this->_curlHandler, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
66+
curl_setopt($this->_curlHandle, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
6767
}
68-
curl_setopt($this->_curlHandler, CURLOPT_HEADERFUNCTION, function($curl, string $header) {
68+
curl_setopt($this->_curlHandle, CURLOPT_HEADERFUNCTION, function($curl, string $header) {
6969
$length = strlen($header);
7070

7171
$headerParts = explode(':', $header, 2);
@@ -165,8 +165,8 @@ public function dutchAddressRD(float $rdX, float $rdY): array
165165
{
166166
$urlParts = [
167167
'nl/v1/addresses/rd',
168-
rawurlencode($rdX),
169-
rawurlencode($rdY),
168+
rawurlencode((string) $rdX),
169+
rawurlencode((string) $rdY),
170170
];
171171

172172
return $this->_performApiCall(implode('/', $urlParts), null);
@@ -179,8 +179,8 @@ public function dutchAddressLatLon(float $latitude, float $longitude): array
179179
{
180180
$urlParts = [
181181
'nl/v1/addresses/latlon',
182-
rawurlencode($latitude),
183-
rawurlencode($longitude),
182+
rawurlencode((string) $latitude),
183+
rawurlencode((string) $longitude),
184184
];
185185

186186
return $this->_performApiCall(implode('/', $urlParts), null);
@@ -261,7 +261,10 @@ public function isValidDutchPostcodeFormat(string $postcode): bool
261261

262262
public function __destruct()
263263
{
264-
curl_close($this->_curlHandler);
264+
if (isset($this->_curlHandle))
265+
{
266+
curl_close($this->_curlHandle);
267+
}
265268
}
266269

267270
protected function _validateSessionHeader(string $session): void
@@ -279,22 +282,22 @@ protected function _validateSessionHeader(string $session): void
279282
protected function _performApiCall(string $path, ?string $session): array
280283
{
281284
$url = static::SERVER_URL . $path;
282-
curl_setopt($this->_curlHandler, CURLOPT_URL, $url);
283-
curl_setopt($this->_curlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
284-
curl_setopt($this->_curlHandler, CURLOPT_USERPWD, $this->_key .':'. $this->_secret);
285+
curl_setopt($this->_curlHandle, CURLOPT_URL, $url);
286+
curl_setopt($this->_curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
287+
curl_setopt($this->_curlHandle, CURLOPT_USERPWD, $this->_key .':'. $this->_secret);
285288
if ($session !== null)
286289
{
287-
curl_setopt($this->_curlHandler, CURLOPT_HTTPHEADER, [
290+
curl_setopt($this->_curlHandle, CURLOPT_HTTPHEADER, [
288291
static::SESSION_HEADER_KEY . ': ' . $session,
289292
]);
290293
}
291294

292295
$this->_mostRecentResponseHeaders = [];
293-
$response = curl_exec($this->_curlHandler);
296+
$response = curl_exec($this->_curlHandle);
294297

295-
$responseStatusCode = curl_getinfo($this->_curlHandler, CURLINFO_RESPONSE_CODE);
296-
$curlError = curl_error($this->_curlHandler);
297-
$curlErrorNr = curl_errno($this->_curlHandler);
298+
$responseStatusCode = curl_getinfo($this->_curlHandle, CURLINFO_RESPONSE_CODE);
299+
$curlError = curl_error($this->_curlHandle);
300+
$curlErrorNr = curl_errno($this->_curlHandle);
298301
if ($curlError !== '')
299302
{
300303
throw new CurlException(vsprintf('Connection error number `%s`: `%s`.', [$curlErrorNr, $curlError]));

0 commit comments

Comments
 (0)