chiro-canto/public/api/v1/records/index.php
2021-04-15 12:29:53 +02:00

75 lines
2.9 KiB
PHP
Executable File

<?php
function fill_record($id, $data) {
global $response;
$response['records'][$id] = array();
$response['records'][$id]['id'] = $data['id'];
$response['records'][$id]['author_id'] = $data['author_id'];
$response['records'][$id]['recordist_name'] = $data['recordist_name'];
$response['records'][$id]['file_name'] = $data['file_name'];
$response['records'][$id]['url'] = "https://".$_SERVER['SERVER_NAME']."/storage/records/".$data['file_name'];
$response['records'][$id]['license'] = $data['license'];
$response['records'][$id]['species'] = $data['species'];
$response['records'][$id]['subspecies'] = $data['subspecies'];
$response['records'][$id]['sound'] = array();
$response['records'][$id]['sound']['type'] = $data['sound_type'];
$response['records'][$id]['sound']['duration'] = $data['duration'];
$response['records'][$id]['sound']['sample_rate'] = $data['sample_rate'];
$response['records'][$id]['sound']['time_expansion'] = $data['time_expansion'];
$response['records'][$id]['recorder'] = array();
$response['records'][$id]['recorder']['make'] = $data['make'];
$response['records'][$id]['recorder']['model'] = $data['model'];
$response['records'][$id]['recorder']['serial'] = $data['serial'];
$response['records'][$id]['location'] = array();
$response['records'][$id]['location']['country'] = $data['country'];
$response['records'][$id]['location']['lat'] = $data['lat'];
$response['records'][$id]['location']['lng'] = $data['lng'];
$response['records'][$id]['datetime'] = array();
$response['records'][$id]['datetime']['date'] = $data['date'];
$response['records'][$id]['datetime']['time'] = $data['time'];
$response['records'][$id]['remarks'] = $data['remarks'];
}
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
header("Content-Type: application/json");
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/database/credentials.php";
// Connect the database
try{
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
}catch (Exception $e){
die("Error : ".$e->getMessage());
}
$response = array();
if (isset($_GET['id'])) {
$req = $db->prepare('SELECT * FROM records WHERE id=:id');
$req->execute(array(
"id"=>$_GET['id']
));
if ($data = $req->fetch()) {
$response['records'] = array();
fill_record(0, $data);
} else {
$response['status'] = "Not found";
$response['message'] = "Can't fetch data for this record id.";
}
} else {
$req = $db->prepare('SELECT * FROM records');
$req->execute();
$result = $req->fetchAll();
$i = 0;
foreach($result as $row) {
fill_record($i, $row);
$i++;
}
}
echo json_encode($response);