2023-09-25 12:43:51 +02:00
|
|
|
<?php
|
2023-09-25 15:45:26 +02:00
|
|
|
include "zefixAPI.php";
|
2023-09-25 12:43:51 +02:00
|
|
|
|
2023-09-25 14:29:53 +02:00
|
|
|
$maxExecutionTime = 80;
|
2023-09-25 13:49:34 +02:00
|
|
|
$taskDir = 'tasks';
|
2023-09-25 15:45:26 +02:00
|
|
|
$downloadDir = 'download';
|
2023-09-25 14:29:53 +02:00
|
|
|
$latesEndTime = time() + $maxExecutionTime;
|
|
|
|
|
2023-09-25 15:45:26 +02:00
|
|
|
|
|
|
|
if(!is_dir($downloadDir)){
|
|
|
|
mkdir($downloadDir, 0755, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function doRequest($data, $filename, $username, $password)
|
2023-09-25 13:49:34 +02:00
|
|
|
{
|
2023-09-25 15:45:26 +02:00
|
|
|
echo "doing Request: ".json_encode($data);
|
|
|
|
echo "<br>";
|
|
|
|
$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);
|
|
|
|
}
|
2023-09-25 13:49:34 +02:00
|
|
|
}
|
2023-09-25 12:43:51 +02:00
|
|
|
|
2023-09-25 13:49:34 +02:00
|
|
|
function sendEmail($emailAddress, $filename)
|
|
|
|
{
|
|
|
|
echo "sending ".$filename." too ".$emailAddress;
|
|
|
|
}
|
2023-09-25 14:29:53 +02:00
|
|
|
// Stop executing bevore reaching PHP Max Execution Time
|
|
|
|
while($latesEndTime - time() > 60){
|
|
|
|
// If the directory for Tasks is already created
|
|
|
|
if(is_dir($taskDir)) {
|
|
|
|
$taskfiles = scandir($taskDir);
|
|
|
|
$taskfiles = array_diff($taskfiles, array('.', '..'));
|
|
|
|
sort($taskfiles);
|
2023-09-25 15:45:26 +02:00
|
|
|
// 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()) {
|
2023-09-25 14:29:53 +02:00
|
|
|
$taskString = file_get_contents($taskDir.'/'.$taskfiles[0]);
|
|
|
|
$task = json_decode($taskString, true);
|
2023-09-25 13:49:34 +02:00
|
|
|
|
2023-09-25 14:29:53 +02:00
|
|
|
// if there are any requests to do, do the first
|
|
|
|
if(count($task['requests']) > 0) {
|
2023-09-25 15:45:26 +02:00
|
|
|
doRequest($task['requests'][0], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]), $username , $password);
|
2023-09-25 14:29:53 +02:00
|
|
|
echo "Request done";
|
2023-09-25 15:45:26 +02:00
|
|
|
echo "<br>";
|
2023-09-25 14:29:53 +02:00
|
|
|
array_shift($task['requests']);
|
|
|
|
$taskString = json_encode($task);
|
|
|
|
$taskfile = fopen($taskDir.'/'.$taskfiles[0], 'w');
|
|
|
|
if($taskfile){
|
|
|
|
fwrite($taskfile, $taskString);
|
|
|
|
fclose($taskfile);
|
|
|
|
}
|
2023-09-25 13:49:34 +02:00
|
|
|
}
|
2023-09-25 14:29:53 +02:00
|
|
|
// if there are no Requests left, send the E-Mail
|
2023-09-25 13:49:34 +02:00
|
|
|
else {
|
2023-09-25 15:45:26 +02:00
|
|
|
sendEmail($task['email'], str_replace(".json", ".csv", $downloadDir.'/'.$taskfiles[0]));
|
2023-09-25 14:29:53 +02:00
|
|
|
echo "Email sent";
|
2023-09-25 15:45:26 +02:00
|
|
|
echo "<br>";
|
2023-09-25 14:29:53 +02:00
|
|
|
unlink($taskDir.'/'.$taskfiles[0]);
|
|
|
|
echo "Task File deleted";
|
2023-09-25 15:45:26 +02:00
|
|
|
echo "<br>";
|
2023-09-25 14:29:53 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// acutal Time - start time
|
|
|
|
echo "nothing to do after: ".strval(time() - ($latesEndTime - $maxExecutionTime));
|
|
|
|
echo "sleeping 10 seconds";
|
2023-09-25 15:45:26 +02:00
|
|
|
echo "<br>";
|
2023-09-25 14:29:53 +02:00
|
|
|
sleep(10);
|
2023-09-25 13:49:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 14:29:53 +02:00
|
|
|
|
2023-09-25 13:49:34 +02:00
|
|
|
?>
|