Skip to content

Commit 4001d51

Browse files
committed
Fix exception in the example proxy when specifying a language for the international API methods
1 parent b33c051 commit 4001d51

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

example/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ any supported method, for example:
1818
`example/index.php?p=dutchAddressByPostcode/2012es/31`
1919

2020
### Show autocomplete results for a German lookup
21-
`example/index.php?p=internationalAutocomplete/deu/strass`
21+
`example/index.php?p=internationalAutocomplete/deu/berlin`
2222

2323
### Show details for a German autocomplete lookup
24-
`example/index.php?p=internationalGetDetails/deu6SVBbpsiLAfbIGnJSvrXjowbUfFAEQxTRQHTsHM9gj76DzsZ6P3BBv8bOknW7MXrI8OJuDanqDp7iy8WwE7woYhDTqHIpSilNHbOTx00CL8QmigIZ1yxr9PNVyRIL9cPQPhwfDpYYo0NgSeI9E`
24+
`example/index.php?p=internationalGetDetails/deu8jKJe6loItZqtGADAIQGhsPCv9lzLDkd8gfldjgkL4auvVBghq9FoiAuO51y2cL1WohglY2INCswqCGlak7NOm30ELGKca7R8pamRPzapHYQRDVC75lB7eDs26l38FuHTw6Ijp2ISEfN8l2thu1kUa`
2525

2626
Please only use this proxy as an example for your own implementation.
2727
What you should add:
2828

29-
* Don't allow random calls but implement what you use in a custom wrapper
30-
* Add caching to prevent fetching the same data over and over
31-
* Add Session by reading `$_COOKIE`, don't pass it in the URL
29+
* Don't allow random calls but implement what you use in a custom wrapper.
30+
* Add caching to prevent fetching the same data over and over.
31+
* Properly implement session identifiers when using the international autocomplete API. Use a new identifier for each address being validated.
3232
* Configure your webserver to send all requests under a certain directory to your proxy
3333
so you don't need to use `?p=`
3434

example/autocomplete.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const inputElement = document.querySelector('.input-autocomplete'),
2020
autocomplete = new PostcodeNl.AutocompleteAddress(inputElement, {
2121
context: 'nld',
22+
language: 'en-GB',
2223
autocompleteUrl: 'index.php?p=internationalAutocomplete', // Required
2324
addressDetailsUrl: 'index.php?p=internationalGetDetails', // Required
2425
});

example/index.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,28 @@
2828
if (0 !== strpos($action, 'international') && 0 !== strpos($action, 'dutchAddress'))
2929
throw new Exception('This example only supports calls to international or dutchAddress methods');
3030

31-
if ($action == 'internationalAutocomplete' || $action == 'internationalGetDetails')
32-
$parts []= 'MY_SESSION_ID';
31+
// Use the session header if it was specified already (the Postcode.nl js library will generate one automatically, for example)
32+
// Fall back to a place holder identifier. Don't use a fixed identifier for production environments, please read:
33+
// https://api.postcode.nl/documentation/international/v1/Autocomplete/autocomplete
34+
$sessionHeaderKey = 'HTTP_' . str_replace('-', '_', strtoupper(PostcodeNl\Api\Client::SESSION_HEADER_KEY));
35+
$sessionId = $_SERVER[$sessionHeaderKey] ?? 'MY_SESSION_ID';
3336

3437
$client = new PostcodeNl\Api\Client(API_KEY, API_SECRET, PLATFORM);
35-
print json_encode(call_user_func_array([$client, $action], $parts));
38+
39+
switch ($action)
40+
{
41+
case 'internationalAutocomplete':
42+
$response = $client->internationalAutocomplete($parts[0], $parts[1], $sessionId, $parts[2] ?? null);
43+
break;
44+
case 'internationalGetDetails':
45+
$response = $client->internationalGetDetails($parts[0], $sessionId);
46+
break;
47+
default:
48+
$response = call_user_func_array([$client, $action], $parts);
49+
break;
50+
}
51+
52+
print json_encode($response);
3653
}
3754
catch (Exception $e)
3855
{

0 commit comments

Comments
 (0)