-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp.php
More file actions
45 lines (37 loc) · 1.33 KB
/
php.php
File metadata and controls
45 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
// Postali API — PHP 7+ using built-in HTTP wrappers (no Composer).
// Run with: php examples/php.php
const BASE = 'https://postali.app/api/v1';
function postali_get(string $path): array {
$body = file_get_contents(BASE . $path);
return json_decode($body, true);
}
function postali_post(string $path, array $payload): array {
$opts = [
'http' => [
'method' => 'POST',
'header' => "Content-Type: application/json\r\n",
'content' => json_encode($payload),
'ignore_errors' => true,
],
];
$body = file_get_contents(BASE . $path, false, stream_context_create($opts));
return json_decode($body, true);
}
function lookup(string $country, string $cp): array {
return postali_get("/$country/cp/$cp");
}
function validate(string $country, string $cp): array {
return postali_get("/$country/validate/$cp");
}
function search(string $country, string $q, int $limit = 10): array {
$qs = http_build_query(['q' => $q, 'limit' => $limit]);
return postali_get("/$country/search?$qs");
}
function bulk(string $country, array $cps): array {
return postali_post("/$country/bulk", ['cps' => $cps]);
}
print_r(lookup('mx', '06700'));
print_r(validate('co', '050001'));
print_r(search('es', 'barcelona', 5));
print_r(bulk('mx', ['06700', '44100', '00000']));