Hello,
I’m trying to get data from collection “vijesti” i created in cockpit backend but cant get it work and i got JSON decode error: Syntax error, Raw response: . I put cockpit files in “cockpit” folder and folder is with other html php js etc… Can you guys help bcs i cant find solution to simple fetch data. Version oh php server is 8.3. Its siple html site with index.php. I would really appreciate your help. Thanks in advance. Php file for fetching:
\<?php
// Show all errors
ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(E_ALL);
// API setup
$api_url = ‘MYLINK/cockpit/api/collections/get/vijesti’;
$api_token = ‘API-ON-PROFILE’;
// Initialize cURL
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
“Cockpit-Token: $api_token”
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($response === false || $http_code !== 200) {
echo “
Greška u povezivanju s CMS-om.
”;echo “
HTTP status: $http_code\ncURL greška: $error\nRaw response:\n” . htmlspecialchars($response) . “”;
exit;
}
// Decode JSON
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "
Greška u dekodiranju JSON odgovora: " . json_last_error_msg() . “
”;echo “
JSON odgovor (raw):\n” . $response . “”;
exit;
}
// Display entries
if (isset($data[‘entries’]) && count($data[‘entries’])) {
foreach ($data[‘entries’] as $entry) {
echo ‘
echo ’
echo ’
’ . htmlspecialchars($entry[‘Naslov’]) . ‘
’;echo ’
’ . nl2br(htmlspecialchars($entry[‘Opis’])) . ‘
’;echo ’
echo ‘
}
} else {
echo ‘
Nema vijesti za prikaz ili prazna kolekcija.
’;echo “
Raw response:\n” . htmlspecialchars($response) . “”;
}
?>