Skip to content

Commit f136478

Browse files
add an extremely simple example proxy and basic usage
1 parent 5c6ca29 commit f136478

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Postcode.nl Api Client
22
=============
33

4-
PHP Api client for the [Postcode.nl Api](https://api.postcode.nl/documentation/).
4+
PHP Api client for the [Postcode.nl Api](https://api.postcode.nl/documentation/).
55

66

77
How to install
88
=============
99

1010
### Composer
1111

12-
Get package `postcode-nl/api-client`.
12+
Get package `postcode-nl/api-client`.
1313

1414
### From this repository
1515

example/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Example Postcode.nl Api Proxy
2+
=============
3+
4+
PHP Api proxy for the [Postcode.nl Api](https://api.postcode.nl/documentation/).
5+
6+
7+
Usage
8+
=============
9+
10+
This directory serves as an extremely simple proxy example for the supplied client.
11+
Set the correct `API_KEY` and `API_SECRET` and optional `PLATFORM` and you can call
12+
any supported method, for example:
13+
14+
### Show all valid data from a valid postal code
15+
`.../example/index.php?action=dutchAddressByPostcode&data[]=2012es&data[]=30`
16+
17+
### Return an exception for non-existing addresses
18+
`.../example/index.php?action=dutchAddressByPostcode&data[]=2012es&data[]=31`
19+
20+
### Show autocomplete results for a German lookup
21+
`.../example/index.php?action=internationalAutocomplete&data[]=deu&data[]=strasse&data[]=CLIENT_SESSION_FROM_COOKIE`
22+
23+
Please only use this proxy as an example for your own implementation.
24+
What you should add:
25+
26+
* Don't allow random calls but implement what you use in a custom wrapper
27+
* Add caching to prevent fetching the same data over and over
28+
* Add Session by reading `$_COOKIE`, don't pass it in the URL
29+
30+
License
31+
=============
32+
33+
The code is available under the Simplified BSD License, see the included LICENSE file.

example/index.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
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');
6+
7+
8+
// this will load exceptions when they occur
9+
spl_autoload_register(function($class){
10+
if (0 === strpos($class, 'PostcodeNl\\'))
11+
require(__DIR__ .'/../src/'. str_replace('\\', '/', $class) .'.php');
12+
});
13+
14+
header('Content-Type: application/json');
15+
16+
try
17+
{
18+
if (0 !== strpos($_GET['action'], 'international') && 0 !== strpos($_GET['action'], 'dutchAddress'))
19+
throw new Exception('This example only supports calls to international or dutchAddress methods');
20+
21+
$client = new PostcodeNl\Api\Client(API_KEY, API_SECRET, PLATFORM);
22+
print json_encode(call_user_func_array([$client, $_GET['action']], $_GET['data'] ?? []));
23+
}
24+
catch (Exception $e)
25+
{
26+
http_response_code(400);
27+
print json_encode(['Exception' => $e->getMessage()]);
28+
}

0 commit comments

Comments
 (0)