genderwatchprotocol/dataCollector/dataCollector.php

65 lines
2.3 KiB
PHP
Raw Normal View History

2023-02-14 19:56:51 +01:00
<?php
2023-02-14 23:47:03 +01:00
require $_SERVER['DOCUMENT_ROOT'] . '/spreadSheetReader/read_write_xlsx.php';
2023-02-14 19:56:51 +01:00
$fileName = $_SERVER['DOCUMENT_ROOT'] . '/data/data.xlsx';
function objectToArray($object, $fullJSON) {
return [$object -> id,
$object -> lastSave,
$fullJSON,
$object -> timeWatchM,
$object -> timeWatchW,
$object -> timeWatchD,
$object -> countWatchM,
$object -> countWatchW,
$object -> countWatchD];
}
2023-02-15 13:31:48 +01:00
$databaseAddress = "127.0.0.1";
$databasePort = "3306";
$databaseName = "u517357132_genderwatchpro";
$databaseUser = "u517357132_genderwatchpro";
$databasePassword = "zU!7gRHA6x";
2023-02-15 13:46:05 +01:00
$link = mysqli_connect($databaseAddress, $databaseUser, $databasePassword, $databaseName);
if (!$link) {
die('Could not connect to Database: ' . mysql_error());
}
//if connection is successfully you will see message below
2023-02-15 13:50:12 +01:00
echo 'Connected successfully to Database<br>';
2023-02-15 13:31:48 +01:00
2023-02-15 13:46:05 +01:00
mysqli_close($link);
//$mysqli = new mysqli($databaseAddress, $databaseUser, $databasePassword, $databaseName);
2023-02-15 13:31:48 +01:00
function saveToDatabase($contentArray, $mysqli) {
2023-02-15 13:50:12 +01:00
echo "sending Query to Database<br>";
2023-02-15 13:46:05 +01:00
$response = mysqli_query($mysqli, "INSERT INTO `topics` (`id`, `fullJSON`, `timeWatchM`,`timeWatchW`, `timeWatchD`, `countWatchM`, `countWatchW`, `countWatchD`) values ($contentArray[0], $contentArray[2], $contentArray[3], $contentArray[4], $contentArray[5], $contentArray[6], $contentArray[7], $contentArray[8]) ON DUPLICATE KEY UPDATE `id` = $contentArray[0]");
2023-02-15 13:50:12 +01:00
echo "Query sent<br>";
2023-02-15 13:41:11 +01:00
echo $response;
return $response;
2023-02-15 13:31:48 +01:00
}
function getFromDatabase() {
2023-02-15 13:40:09 +01:00
return [];
2023-02-14 19:56:51 +01:00
}
2023-02-14 23:47:03 +01:00
$entityBody = '{"lastSave": 0, "timeWatchM": 0, "timeWatchW": 0, "timeWatchD": 0, "countWatchM": 0, "countWatchW": 0, "countWatchD": 0, "id":"test","watches":[{"actions":[]},{"actions":[]},{"actions":[]}]}';
2023-02-15 00:28:12 +01:00
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
2023-02-14 19:56:51 +01:00
$entityBody = file_get_contents('php://input');
2023-02-14 23:47:03 +01:00
$entityBodyObject = json_decode($entityBody);
2023-02-14 19:56:51 +01:00
2023-02-14 23:47:03 +01:00
echo "POST Request received.<br>";
2023-02-14 19:56:51 +01:00
2023-02-14 23:47:03 +01:00
if(property_exists($entityBodyObject, "id")) {
2023-02-15 13:46:05 +01:00
saveToDatabase(objectToArray($entityBodyObject, $entityBody), $link);
2023-02-14 23:47:03 +01:00
echo "<br>Data saved.<br>";
}
2023-02-15 13:31:48 +01:00
} elseif($_SERVER['REQUEST_METHOD'] === 'GET') {
download_xlsxFile(getFromDatabase());
2023-02-15 00:28:12 +01:00
} else {
echo "invalid Request.<br> Type: ".$_SERVER['REQUEST_METHOD'];
2023-02-14 19:56:51 +01:00
}
2023-02-15 13:46:05 +01:00
mysqli_close( );
2023-02-14 23:47:03 +01:00