organic: Enhance external apis

This commit is contained in:
2025-12-22 16:56:23 +01:00
parent 551ddd3c11
commit 0cd3b3de8e
10 changed files with 350 additions and 110 deletions

View File

@@ -1,11 +1,15 @@
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');
$base = 'https://' . $_SERVER['SERVER_NAME'] . explode('?', $_SERVER['REQUEST_URI'])[0];
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
header('Status: 405');
header('Content-Length: 0');
header('Content-Length: 56');
header('Allow: GET, HEAD');
echo "{\"error\":\"method not allowed\",\"allow\":[\"GET\",\"HEAD\"]}\n";
exit;
}
@@ -16,17 +20,84 @@ function jenc($data): string {
$info = $_SERVER['PATH_INFO'];
if ($info !== '') {
$id = substr($info, 1);
header('Status: 404');
header('Content-Length: 0');
$url = "https://www.bioc.info/search/producerdetail?producer=" . urlencode($id);
$s = curl_init($url);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
if (($html = curl_exec($s)) === false) {
header('Status: 500');
header('Content-Length: 34');
echo "{\"error\":\"internal server error\"}\n";
exit;
}
$p1 = strpos($html, '<main>');
$p2 = strpos($html, '</main>');
if ($p1 === false || $p2 === false) {
header('Status: 404');
header('Content-Length: 22');
echo "{\"error\":\"not found\"}\n";
exit;
}
$html = substr($html, $p1 + 6, $p2 - $p1 - 6);
$data = [];
preg_match_all('@<div class="row">\s*<div class="col[^>]*?>\s*(.*?)\s*</div>\s*<div class="col[^>]*?>\s*(.*?)\s*</div>\s*</div>@s', $html, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$data[$m[1]] = trim(preg_replace('/\s*\n\s*/', "\n", html_entity_decode(preg_replace('/<.*?>/', '', str_replace('<br/>', "\n", preg_replace('/\s+/', ' ', trim($m[2])))))));
}
$certRows = [];
preg_match_all('@<tr>\s*<td.*?href="([^"]*)">\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*</tr>@s', $html, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$cert = [];
foreach (array_splice($m, 1) as $s) {
$cert[] = trim(preg_replace('/\s*\n\s*/', "\n", preg_replace('/<.*?>/', '', str_replace('<br/>', "\n", preg_replace('/\s+/', ' ', trim(html_entity_decode($s)))))));
}
$certRows[] = $cert;
}
$lines = explode("\n", $data['Address']);
[$cc, $_, $postalCode, $city] = explode(' ', $lines[2], 4);
$certs = [];
foreach ($certRows as $row) {
[$nr, $lang] = explode('(', $row[1], 2);
[$from, $to] = explode(' - ', $row[3], 2);
$certs[] = [
'nr' => trim($nr),
'language' => substr($lang, 0, 2),
'pdfUrl' => $row[0],
'type' => $row[2],
'validFrom' => date('Y-m-d', strtotime($from)),
'validUntil' => date('Y-m-d', strtotime($to)),
];
}
[$authCode, $authName] = explode(' ', $data['Certification body'], 2);
echo jenc([
'id' => $id,
'idNr' => $data['Operator ID'] ?? null,
'name' => $lines[0],
'address' => $lines[1],
'postalCode' => $postalCode,
'city' => $city,
'countryCode' => $cc,
'authority' => ['code' => $authCode, 'name' => $authName],
'url' => $base,
'originUrl' => $url,
'certificates' => $certs,
]);
exit;
}
$source_groups = [
[ '',
'0892fdd7-6649-cb3b-75ef-a60649cfebfd', 'fde4ccef-0f36-4db9-9f2f-9bb6009c872a',
'8e6d20d3-6bbe-c7ae-7a77-fb7fa80ee291', '830b3bd7-f1da-e20e-300d-e561bfb8205b',
'ed025d59-056f-ffd0-79e0-05348fe88d59', '95d536c8-c869-9f72-1bce-4256a2a4ce49'
], [ '',
['', '0892fdd7-6649-cb3b-75ef-a60649cfebfd'],
['', 'fde4ccef-0f36-4db9-9f2f-9bb6009c872a'],
['', '8e6d20d3-6bbe-c7ae-7a77-fb7fa80ee291'],
['', '830b3bd7-f1da-e20e-300d-e561bfb8205b'],
['', 'ed025d59-056f-ffd0-79e0-05348fe88d59'],
['', '95d536c8-c869-9f72-1bce-4256a2a4ce49'],
['',
'bef6b49a-3186-ba7f-fffb-281bfb4ca2d2', 'fc7a0cc3-c4b4-a4fb-5b7e-5cf9b68ed198',
'9aecd829-8f3c-6829-449f-9cb511e0c1ff', 'e7a7089b-0fd7-60b9-48af-c0988a90ec37',
'63d570d8-0ab2-b793-fe11-62e0777e4236'
@@ -43,19 +114,37 @@ $idNr = $_GET['idNr'] ?? null;
if ($country === null) {
header('Status: 400');
header('Content-Length: 0');
header('Content-Length: 88');
echo "{\"error\":\"bad request\",\"message\":\"The 'country' request URL query parameter is required\"}\n";
exit;
}
$data = [];
$url = "https://www.bioc.info/search/producerSearchQuery?search[name]=" . urlencode($name) . "&search[citycode]=" . urlencode($postalCode) . "&search[operatorId]=" . urlencode($idNr) . "&search[country]=$country";
if ($_GET['allDBs'] === 'true') {
$s = curl_init($url . implode('&sources[]=', $source_groups[0]));
$m = curl_multi_init();
$requests = [];
foreach ($source_groups as $group) {
$s = curl_init($url . implode('&sources[]=', $group));
$requests[] = $s;
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
if (($json = curl_exec($s)) === false) {
curl_multi_add_handle($m, $s);
}
$running = 1;
do {
curl_multi_exec($m, $running);
} while ($running);
foreach ($requests as $r) {
curl_multi_remove_handle($m, $r);
}
curl_multi_close($m);
foreach ($requests as $s) {
if (($json = curl_multi_getcontent($s)) === false) {
header('Status: 500');
header('Content-Length: 0');
header('Content-Length: 34');
echo "{\"error\":\"internal server error\"}\n";
exit;
}
$res = json_decode($json, true)['results'];
@@ -64,18 +153,6 @@ if ($_GET['allDBs'] === 'true') {
}
}
$s = curl_init($url . implode('&sources[]=', $source_groups[1]));
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
if (($json = curl_exec($s)) === false) {
header('Status: 500');
header('Content-Length: 0');
exit;
}
$res = json_decode($json, true)['results'];
foreach ($res as $r) {
$data[$r['id']] = $r['row'];
}
header('Content-Type: application/json; charset=UTF-8');
$first = true;
@@ -90,8 +167,8 @@ foreach ($data as $id => $row) {
'name' => substr($row[0], strrpos($row[0], '>') + 1),
'postalCode' => substr($row[1], strrpos($row[1], '>') + 1),
'city' => substr($row[2], strrpos($row[2], '>') + 1),
'authorityCode' => $auth[0],
'authorityName' => $auth[1],
'authority' => ['code' => $auth[0], 'name' => $auth[1]],
'url' => "$base/$id",
]);
$first = false;
}