Skip to content

Commit 898a9aa

Browse files
update example: make compatible with IAA javascripts
1 parent f136478 commit 898a9aa

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

example/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ Set the correct `API_KEY` and `API_SECRET` and optional `PLATFORM` and you can c
1212
any supported method, for example:
1313

1414
### Show all valid data from a valid postal code
15-
`.../example/index.php?action=dutchAddressByPostcode&data[]=2012es&data[]=30`
15+
`example/index.php?p=dutchAddressByPostcode/2012es/30`
1616

1717
### Return an exception for non-existing addresses
18-
`.../example/index.php?action=dutchAddressByPostcode&data[]=2012es&data[]=31`
18+
`example/index.php?p=dutchAddressByPostcode/2012es/31`
1919

2020
### Show autocomplete results for a German lookup
21-
`.../example/index.php?action=internationalAutocomplete&data[]=deu&data[]=strasse&data[]=CLIENT_SESSION_FROM_COOKIE`
21+
`example/index.php?p=internationalAutocomplete/deu/strasse/CLIENT_SESSION_FROM_COOKIE`
2222

2323
Please only use this proxy as an example for your own implementation.
2424
What you should add:
2525

2626
* Don't allow random calls but implement what you use in a custom wrapper
2727
* Add caching to prevent fetching the same data over and over
2828
* Add Session by reading `$_COOKIE`, don't pass it in the URL
29+
* Configure your webserver to send all requests under a certain directory to your proxy
30+
so you don't need to use `?p=`
2931

3032
License
3133
=============

example/autocomplete.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="nl-NL">
3+
<head>
4+
<style>
5+
input[type=text]{
6+
width: 500px;
7+
}
8+
</style>
9+
<link rel="stylesheet" href="https://api.postcode.nl/static/3.2.16.0/css/autocomplete-address.css">
10+
<script src="https://api.postcode.nl/static/3.2.16.0/js/PostcodeNl/AutocompleteAddress.min.js"></script>
11+
<script src="https://api.postcode.nl/static/3.2.16.0/js/PostcodeNl/AutocompleteAddress.js"></script>
12+
</head>
13+
<body>
14+
<form>
15+
<input type="text" class="input-autocomplete" placeholder="City, street or postcode">
16+
</form>
17+
18+
<address class="autocomplete-result autocomplete-result-int"></address>
19+
<script>
20+
const inputElement = document.querySelector('.input-autocomplete'),
21+
autocomplete = new PostcodeNl.AutocompleteAddress(inputElement, {
22+
autocompleteUrl: 'index.php?p=internationalAutocomplete', // Required
23+
addressDetailsUrl: 'index.php?p=internationalGetDetails', // Required
24+
});
25+
26+
inputElement.addEventListener('autocomplete-select', function (e) {
27+
console.log('Selected item:', e.detail);
28+
});
29+
</script>
30+
</body>

example/index.php

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

3-
define('API_KEY', /* as received by mail after signing up */);
4-
define('API_SECRET', /* as received by mail after signing up */);
5-
define('PLATFORM', 'example proxy');
3+
define('API_KEY', '** insert key here **');
4+
define('API_SECRET', '** insert secret here **');
5+
define('PLATFORM', 'example proxy');
66

77

88
// this will load exceptions when they occur
@@ -15,11 +15,24 @@
1515

1616
try
1717
{
18-
if (0 !== strpos($_GET['action'], 'international') && 0 !== strpos($_GET['action'], 'dutchAddress'))
18+
if (!isset($_GET['p']))
19+
throw new Exception('Missing parameters');
20+
21+
$parts = explode('/', $_GET['p']);
22+
23+
if (count($parts) < 2)
24+
throw new Exception('Not enough parameters');
25+
26+
$action = array_shift($parts);
27+
28+
if (0 !== strpos($action, 'international') && 0 !== strpos($action, 'dutchAddress'))
1929
throw new Exception('This example only supports calls to international or dutchAddress methods');
2030

31+
if ($action == 'internationalAutocomplete' || $action == 'internationalGetDetails')
32+
$parts []= 'MY_SESSION_ID';
33+
2134
$client = new PostcodeNl\Api\Client(API_KEY, API_SECRET, PLATFORM);
22-
print json_encode(call_user_func_array([$client, $_GET['action']], $_GET['data'] ?? []));
35+
print json_encode(call_user_func_array([$client, $action], $parts));
2336
}
2437
catch (Exception $e)
2538
{

0 commit comments

Comments
 (0)