From 3fa9fa6d793a9660c94f98a78e8e0760c3a3bdc9 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 25 Sep 2023 15:45:26 +0200 Subject: [PATCH] prepared Task Executer --- submit.php | 3 +-- taskExecuter.php | 53 ++++++++++++++++++++++++++++++++++++++++++------ zefixAPI.php | 11 +++++----- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/submit.php b/submit.php index 91d2d69..81d4f79 100644 --- a/submit.php +++ b/submit.php @@ -54,7 +54,6 @@ $requests_to_do = Array(); // Loop through the selected values foreach ($sitze as $sitz) { $data["legalSeatId"] = $sitz; - //$response = sendAPICompanySearchRequest($username, $password, $data); $requests_to_do[] = $data; } @@ -66,7 +65,7 @@ $taskString = json_encode($taskdata); if(!is_dir($taskDir)){ mkdir($taskDir, 0755, true); } -$taskfilename = $taskDir.'/'.time().'-'.bin2hex(random_bytes(4)).'.txt'; +$taskfilename = $taskDir.'/'.time().'-'.bin2hex(random_bytes(4)).'.json'; $taskfile = fopen($taskfilename, 'w'); if($taskfile){ fwrite($taskfile, $taskString); diff --git a/taskExecuter.php b/taskExecuter.php index 217c89a..ee2d852 100644 --- a/taskExecuter.php +++ b/taskExecuter.php @@ -1,12 +1,49 @@ "; + $response = sendAPICompanySearchRequest($username, $password, $data); + $companyArray = json_decode($response, true); + // TODO implement + $companyData = [ + ['John', 30, 'New York'], + ['Alice', 25, 'Los Angeles'], + ['Bob', 35, 'Chicago'], + ]; + $companyData = array(); + foreach ($companyArray as $company) { + $companyData[] = [$company['name'],$company['ehraid'],$company['uid'],$company['chid'],$company['legalSeat'],$company['registryOfCommerceId'],$company['legalForm']['name']['de'],$company['status'],$company['sogcDate'],$company['deletionDate']]; + } + if (! file_exists($filename)) { + // If File doesn't exist yet + $file = fopen($filename, 'w'); + if($file) { + // TODO change headers + fputcsv($file, ['name', 'ehraid', 'uid', 'chid', 'legalSeat', 'registryOfCommerceId', 'legalForm', 'status', 'sogcDate', 'deletionDate']); + } + } else { + // If file already exists + $file = fopen($filename, 'a'); + } + if ($file) { + foreach ($companyData as $row){ + fputcsv($file, $row); + } + fclose($file); + } } function sendEmail($emailAddress, $filename) @@ -20,15 +57,16 @@ while($latesEndTime - time() > 60){ $taskfiles = scandir($taskDir); $taskfiles = array_diff($taskfiles, array('.', '..')); sort($taskfiles); - // if there are any taks which are older then 2 Minutes. To be sure the file writing process is finished. - if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 120 < time()) { + // if there are any taks which are older then 1 Minutes. To be sure the file writing process is finished. + if(count($taskfiles) > 0 && intval(explode("-", $taskfiles[0])[0]) + 60 < time()) { $taskString = file_get_contents($taskDir.'/'.$taskfiles[0]); $task = json_decode($taskString, true); // if there are any requests to do, do the first if(count($task['requests']) > 0) { - doRequest($task['requests'][0]); + doRequest($task['requests'][0], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), $username , $password); echo "Request done"; + echo "
"; array_shift($task['requests']); $taskString = json_encode($task); $taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w'); @@ -39,15 +77,18 @@ while($latesEndTime - time() > 60){ } // if there are no Requests left, send the E-Mail else { - sendEmail($task['email'], $taskDir.'/'.$taskfiles[0]); + sendEmail($task['email'], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0])); echo "Email sent"; + echo "
"; unlink($taskDir.'/'.$taskfiles[0]); echo "Task File deleted"; + echo "
"; } } else { // acutal Time - start time echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime)); echo "sleeping 10 seconds"; + echo "
"; sleep(10); } } diff --git a/zefixAPI.php b/zefixAPI.php index 2361d34..d0339ac 100644 --- a/zefixAPI.php +++ b/zefixAPI.php @@ -11,20 +11,19 @@ $password = getenv("password"); * @param array $data * @return bool|string */ -function sendAPICompanySearchRequest(string $username, string $password, array $data): string|bool -{ +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/ZefixPublicREST/api/v1/company/search'; // Headers -$headers = array( + $headers = array( "accept: application/json", "Content-Type: application/json" ); // Initialize cURL session -$ch = curl_init(); - + $ch = curl_init(); // Set cURL options curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); curl_setopt($ch, CURLOPT_URL, $apiUrl);