trying to fetch caldav events and display on wordpress-page. not sure how to to this best (sabre? wordpress http calls? i get “error: no events found”. any help appreciated. tried these two scripts:
<?php
/**
* Plugin Name: Custom Calendar Plugin
* Description: Fetches events from Nextcloud CalDAV calendar and displays them in a table.
* Version: 1.0
* Author: Your Name
*/
add_shortcode('custom_calendar', 'display_custom_calendar');
// Shortcode callback function.
function display_custom_calendar() {
$nextcloud_url = 'https://cloud.chole.ch/remote.php/dav/calendars/Daniel/gigs/';
$username = '*******';
$password = '*******';
// Load Sabre library.
require_once get_stylesheet_directory() . '/custom-calendar-plugin/vendor/autoload.php';
// Initialize the CalDAV client.
$settings = array(
'baseUri' => $nextcloud_url,
'userName' => $username,
'password' => $password,
);
$client = new Sabre\DAV\Client($settings);
try {
// Fetch all events from the calendar.
$calendar_url = $nextcloud_url . 'calendar/';
$calendar_data = $client->propFind($calendar_url, array('{urn:ietf:params:xml:ns:caldav}calendar-data'));
$events = $calendar_data[$calendar_url]['{urn:ietf:params:xml:ns:caldav}calendar-data'];
// Parse the events data.
$xml = new SimpleXMLElement($events);
$events_array = json_decode(json_encode($xml), true);
// Prepare the table.
$table_html = '<table>';
$table_html .= '<tr><th>Event Name</th><th>Start Time</th><th>End Time</th></tr>';
foreach ($events_array['vevent'] as $event) {
$event_name = $event['summary'];
$start_time = date('Y-m-d H:i:s', strtotime($event['dtstart']));
$end_time = date('Y-m-d H:i:s', strtotime($event['dtend']));
// Add the event to the table.
$table_html .= "<tr><td>$event_name</td><td>$start_time</td><td>$end_time</td></tr>";
}
$table_html .= '</table>';
} catch (Exception $e) {
// Handle exceptions if any.
$table_html = 'Error fetching events: ' . $e->getMessage();
}
return $table_html;
}
2nd script tried:
<?php
/*
Template Name: Nextcloud Events
*/
get_header(); // Include the header file
// Replace 'YOUR_NEXTCLOUD_URL', 'YOUR_USERNAME', and 'YOUR_PASSWORD' with your Nextcloud credentials
$nextcloud_url = 'https://cloud.chole.ch/remote.php/dav/calendars/Daniel/gigs/';
$nextcloud_username = '*****';
$nextcloud_password = '*****';
// Fetch events from Nextcloud CalDAV using cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $nextcloud_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$nextcloud_username:$nextcloud_password");
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL error: ' . curl_error($ch);
}
curl_close($ch);
// Parse the XML response
$xml = simplexml_load_string($response);
$events = $xml->children('urn:ietf:params:xml:ns:caldav')->calendar_data->children('urn:ietf:params:xml:ns:caldav')->comp;
// Display the events as a list
echo '<ul>';
foreach ($events as $event) {
$event_details = $event->children('urn:ietf:params:xml:ns:caldav')->comp->vevent;
$event_summary = (string)$event_details->summary;
$event_start = (string)$event_details->dtstart;
$event_end = (string)$event_details->dtend;
echo '<li>';
echo '<strong>' . $event_summary . '</strong><br>';
echo 'Start: ' . $event_start . '<br>';
echo 'End: ' . $event_end . '<br>';
echo '</li>';
}
echo '</ul>';
get_footer(); // Include the footer file