EXAMPLE #03
PHP
Bash
Batch of events in a loop
Push many events back-to-back from a single script.
03-send-batch-events.php
#!/usr/bin/env php
<?php
/**
* Example: Send a batch of Events in a loop
*
* Demonstrates sending multiple events to the same DataStream, such as
* periodic sensor readings or metrics collected over time.
*
* Usage:
* php 03-send-batch-events.php
*/
$baseUrl = 'https://www.spaceplace.app';
$dataStreamId = 'YOUR-DATASTREAM-UUID-HERE';
$tokenValue = 'YOUR-TOKEN-VALUE-HERE';
// Simulate 10 temperature readings taken every 5 minutes
$startTime = strtotime('2026-03-31 09:00:00');
$readings = 10;
$intervalSec = 300; // 5 minutes
$success = 0;
$failed = 0;
for ($i = 0; $i < $readings; $i++) {
$timestamp = date('Y-m-d H:i:s', $startTime + ($i * $intervalSec));
$temperature = round(68 + (mt_rand(-30, 30) / 10), 1); // 65.0 - 71.0
$event = [
'timestamp' => $timestamp,
'number' => $i + 1,
'source' => 'sensor-array-A',
'key' => 'temperature',
'value' => (string) $temperature,
];
$ch = curl_init("{$baseUrl}/api/events");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
"X-DataStream-ID: {$dataStreamId}",
"X-Token: {$tokenValue}",
],
CURLOPT_POSTFIELDS => json_encode($event),
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 201) {
$body = json_decode($response, true);
echo "[{$timestamp}] Sent: {$temperature}F (Event ID: {$body['event_id']})\n";
$success++;
} else {
echo "[{$timestamp}] FAILED (HTTP {$httpCode})\n";
$failed++;
}
}
echo "\nDone: {$success} sent, {$failed} failed.\n";
03-send-batch-events.sh
#!/usr/bin/env bash
#
# Example: Send a batch of Events in a loop
#
# Demonstrates sending multiple events to the same DataStream, such as
# periodic sensor readings or metrics collected over time.
#
# Usage:
# bash 03-send-batch-events.sh
BASE_URL="https://www.spaceplace.app"
DATASTREAM_ID="YOUR-DATASTREAM-UUID-HERE"
TOKEN_VALUE="YOUR-TOKEN-VALUE-HERE"
# Simulate 10 temperature readings taken every 5 minutes
START_TIME=$(date -j -f "%Y-%m-%d %H:%M:%S" "2026-03-31 09:00:00" "+%s" 2>/dev/null \
|| date -d "2026-03-31 09:00:00" "+%s") # macOS || Linux
READINGS=10
INTERVAL=300 # 5 minutes
success=0
failed=0
for i in $(seq 0 $((READINGS - 1))); do
ts=$((START_TIME + i * INTERVAL))
# Format timestamp (macOS / Linux)
timestamp=$(date -r "$ts" "+%Y-%m-%d %H:%M:%S" 2>/dev/null \
|| date -d "@$ts" "+%Y-%m-%d %H:%M:%S")
# Random temperature between 65.0 and 71.0
temp=$(awk "BEGIN { printf \"%.1f\", 68 + ($(( RANDOM % 61 )) - 30) / 10 }")
response=$(curl -s -w "\n%{http_code}" \
-X POST "${BASE_URL}/api/events" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "X-DataStream-ID: ${DATASTREAM_ID}" \
-H "X-Token: ${TOKEN_VALUE}" \
-d "{
\"timestamp\": \"${timestamp}\",
\"number\": $((i + 1)),
\"source\": \"sensor-array-A\",
\"key\": \"temperature\",
\"value\": \"${temp}\"
}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" = "201" ]; then
event_id=$(echo "$body" | jq -r '.event_id')
echo "[${timestamp}] Sent: ${temp}F (Event ID: ${event_id})"
success=$((success + 1))
else
echo "[${timestamp}] FAILED (HTTP ${http_code})"
failed=$((failed + 1))
fi
done
echo ""
echo "Done: ${success} sent, ${failed} failed."