Rest api to calendar service

I have a wakeup routine that tells me the 1st calendar item of the day. my voicemail plays the latest news and then music starts.

I can collect all calendar items of the day with the service call. But i cant get it to work in a rest api. error 500 I have a remote script building up a playlist with all the items needed to played for the wakeup routine.

Does someone has a working example i can use?

#!/usr/bin/php
<?php
include("/opt/sascha/common.php");

// Define the service data
$service_data = array(
    'start_date_time' => date('2024-04-20 00:00:00'),
    'end_date_time' => date('2024-05-30 23:59:59'),
    'target' => array(
        'entity_id' => array(
            'calendar.persoonlijk',
            'calendar.verjaardagen',
            'calendar.feestdagen_in_nederland'
        )
    ),
    'response_mode' => 'queued'
);

// Convert service data to JSON
$service_data_json = json_encode($service_data);

// Define the headers
$headers = array(
    'Authorization: Bearer ' . $sHomeApiKey,
    'Content-Type: application/json'
);

// Make the POST request
$ch = curl_init($sHomeApiUrl . '/api/services/calendar/get_events');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $service_data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Execute the request
$response = curl_exec($ch);

// Check for errors
if(curl_errno($ch)){
    echo 'Error: ' . curl_error($ch);
}

// Close the request
curl_close($ch);

// Output the response
echo $response;
?>

Are you sure your $sHomeApiUrl is correct in terms of HTTP vs HTTPS, port numbers etc?

Yes i use the same setup for other api calls.

Do you want a working get sensor example?

No: the fact you have it working for other calls is useful information.

the feature is and will not be applyed to rest according to git.

I use the websocket.

Here is a working php snippet.

#!/usr/bin/php
<?php

require __DIR__ . '/vendor/autoload.php'; // Include Composer's autoloader

use WebSocket\Client;
use WebSocket\TimeoutException;

// Define the access token
$access_token = $sHomeApiKey;

// Create a WebSocket client
$ws = new Client("ws://HOMEASSISTANTURL:8123/api/websocket");

// Function to send a message
function sendMessage($ws, $message) {
    $ws->send(json_encode($message));
}

// Function to handle WebSocket events
function onMessage($msg) {
    // Handle incoming messages
    echo "Received message: $msg\n";
}

try {
    // Authentication phase
    $auth_message = array(
        "type" => "auth",
        "access_token" => ACCESS_TOKEN
    );
    sendMessage($ws, $auth_message);

    // Wait for authentication response
    $msg = $ws->receive();
    //onMessage($msg);

    // Define the service call message
    $service_call_message = array(
        "id" => 3,
        "type" => "call_service",
        "domain" => "calendar",
        "service" => "get_events",
        "target" => array(
            "entity_id" => array(
                "calendar.feestdagen_in_nederland",
                "calendar.persoonlijk",
                "calendar.verjaardagen"
            )
        ),
        "service_data" => array(
            "start_date_time" => "2024-04-23 00:00:00",
            "end_date_time" => "2024-04-30 00:00:00"
        ),
        "return_response" => true
    );

    // Send the service call message
    sendMessage($ws, $service_call_message);

    // Wait for service response
    while (true) {
        $msg = $ws->receive();
        onMessage($msg);

        // Check if the received message is the service response
        $response = json_decode($msg, true);
        if (isset($response['id']) && $response['id'] == 3 && $response['type'] == 'result') {
            echo "Service response: " . json_encode($response['result']) . "\n";
            break;
        }
    }
} catch (TimeoutException $e) {
    echo "Timeout occurred. Closing connection...\n";
} finally {
    // Close the WebSocket connection
    $ws->close();
    
    // Exit the script
    exit();
}
?>

composer require textalk/websocket