From 6ec770f27c18744496076fa7eb78298fafd9c4af Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 26 Sep 2023 17:30:35 +0200 Subject: [PATCH] fixing API Problems when to many results --- index.php | 2 +- submit.php | 15 ++++++++++----- taskExecuter.php | 21 +++++++++++++++++---- zefixAPI.php | 8 ++++---- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/index.php b/index.php index ff41528..586af7f 100644 --- a/index.php +++ b/index.php @@ -183,7 +183,7 @@ include "zefixAPI.php"; const lines = communityCsvData.trim().split('\n'); const headers = lines[0].split(','); const gemeindeNameIndex = headers.indexOf('Gemeindename'); - const bfsIdIndex = headers.indexOf('bfsId'); + const bfsIdIndex = headers.indexOf('id'); const kantonIndex = headers.indexOf('Kanton'); const registryOfCommerceIdIndex = headers.indexOf('registryOfCommerceId'); diff --git a/submit.php b/submit.php index bc9326e..8d1a686 100644 --- a/submit.php +++ b/submit.php @@ -11,7 +11,9 @@ $taskDir = 'tasks'; */ // Request data $data = array(); - +$data["maxEntries"] = 50; +$data["offset"] = 0; +$data["languageKey"] = "de"; $results = array(); // Wenn Firmenname vorhanden @@ -21,11 +23,14 @@ if(isset($_POST['firma']) && !empty($_POST['firma'])) { // Wenn gelöschte auch gesucht werden sollen if(isset($_POST['geloeschteRechtseinheiten'])) { - $data["activeOnly"] = false; + $data["deletedFirms"] = true; } else { - $data["activeOnly"] = true; + $data["deletedFirms"] = false; } +//TODO get from Frontend +$data["searchType"] = "exact"; + $rechtsformen = array(); if (isset($_POST['rechtsformen']) && is_array($_POST['rechtsformen']) && count($_POST['rechtsformen']) > 0) { $rechtsformen = $_POST['rechtsformen']; @@ -49,11 +54,11 @@ if (isset($_POST['sitze']) && is_array($_POST['sitze']) && count($_POST['sitze'] $requests_to_do = Array(); // Loop through the selected values foreach ($rechtsformen as $rechtsform) { - $data["legalFormId"] = $rechtsform; + $data["legalForms"] = Array(intval($rechtsform)); // Loop through the selected values foreach ($sitze as $sitz) { - $data["legalSeatId"] = $sitz; + $data["legalSeats"] = Array(intval($sitz)); $requests_to_do[] = $data; } diff --git a/taskExecuter.php b/taskExecuter.php index f4238af..1324317 100644 --- a/taskExecuter.php +++ b/taskExecuter.php @@ -4,7 +4,7 @@ include "read_write_xlsx.php"; include "emailSender.php"; -$maxExecutionTime = 480; +$maxExecutionTime = 300; $taskDir = 'tasks'; $downloadDir = 'download'; $minTaskOldness = 10; @@ -18,7 +18,11 @@ if(!is_dir($downloadDir)){ function doRequest($data, $filename, $username, $password) { $response = sendAPICompanySearchRequest($username, $password, $data); - $companyArray = json_decode($response, true); + $responseObject = json_decode($response, true); + if(array_key_exists("error", $responseObject)) { + return false; + } + $companyArray = $responseObject['list']; $companyData = array(); foreach ($companyArray as $company) { $companyFullData = json_decode(sendAPICompanyInfoRequest($username, $password, $company['uid']), true)[0]; @@ -40,6 +44,11 @@ function doRequest($data, $filename, $username, $password) } fclose($file); } + if($responseObject['hasMoreResults']) { + return $responseObject['maxOffset'] + 50; + } else { + return false; + } } @@ -75,8 +84,12 @@ while($latesEndTime - time() > 60){ // if there are any requests to do, do the first if(count($task['requests']) > 0) { - doRequest($task['requests'][0], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), $username , $password); - array_shift($task['requests']); + $nextOffset = doRequest($task['requests'][0], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), $username , $password); + if($nextOffset){ + $task['requests'][0]['offset'] = $nextOffset; + } else { + array_shift($task['requests']); + } $taskString = json_encode($task); $taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w'); if($taskfile){ diff --git a/zefixAPI.php b/zefixAPI.php index b06c22c..588424f 100644 --- a/zefixAPI.php +++ b/zefixAPI.php @@ -44,7 +44,7 @@ function sendAPICompanyInfoRequest(string $username, string $password, string $u function sendAPICompanySearchRequest(string $username, string $password, array $data): string|bool { // API endpoint - $apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/company/search'; + $apiUrl = 'https://www.zefix.admin.ch/ZefixREST/api/v1/firm/search.json'; // Headers $headers = array( @@ -77,7 +77,7 @@ curl_close($ch); function sendAPICommunityRequest(string $username, string $password): string|bool { // API endpoint - $apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/community'; + $apiUrl = 'https://www.zefix.admin.ch/ZefixREST/api/v1/community.json'; // Headers $headers = array( @@ -110,7 +110,7 @@ function sendAPICommunityRequest(string $username, string $password): string|boo function communityCSV(string $username, string $password): string { $response = sendAPICommunityRequest($username, $password); $communityArray = json_decode($response, true); - $csvOutput = 'bfsId,Kanton,Gemeindename,registryOfCommerceId\n'; + $csvOutput = 'id,bfsId,Kanton,Gemeindename,registryOfCommerceId,replacedById,alternateNames\n'; // Create CSV rows foreach ($communityArray as $item) { @@ -122,7 +122,7 @@ function communityCSV(string $username, string $password): string { function sendAPILegalFormRequest(string $username, string $password): string|bool { // API endpoint - $apiUrl = 'https://www.zefix.admin.ch/ZefixPublicREST/api/v1/legalForm'; + $apiUrl = 'https://www.zefix.admin.ch/ZefixREST/api/v1/legalForm.json'; // Headers $headers = array(