<?php
// Configuration
$simpleSpaApiUrl = 'https://my.simplespa.com/api/v1/clients.php';
$simpleSpaApiKey = 'YOUR_SIMPLESPA_API_KEY_HERE';
$openaiApiKey = 'YOUR_OPENAI_API_KEY_HERE';
$openaiModel = 'gpt-4'; // or 'gpt-3.5-turbo' if you want cheaper
$todayMonth = date('n'); // 1-12
$todayDay = date('j'); // 1-31
// 1. Fetch clients with birthday today
$clients = fetchClientsWithBirthdayToday($simpleSpaApiUrl, $simpleSpaApiKey, $todayMonth, $todayDay);
if (empty($clients)) {
echo "No clients found with birthday today.\n";
exit;
}
foreach ($clients as $client) {
$firstname = $client['firstname'];
$email = $client['email'];
// 2. Generate a birthday message with OpenAI
$message = generateBirthdayMessage($firstname, $openaiApiKey, $openaiModel);
// Output
echo "🎉 Birthday Message for {$firstname} ({$email}):\n";
echo $message . "\n\n";
}
// Functions
function fetchClientsWithBirthdayToday($apiUrl, $apiKey, $month, $day) {
$postData = json_encode([
'dob_month' => (int)$month,
'dob_day' => (int)$day,
'page' => 1,
'per_page' => 100
]);
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'SimpleSpa API error: ' . curl_error($ch) . "\n";
return [];
}
curl_close($ch);
$data = json_decode($response, true);
if (isset($data['clients']) && is_array($data['clients'])) {
return $data['clients'];
}
return [];
}
function generateBirthdayMessage($clientName, $openaiKey, $model) {
$prompt = "Write a short, warm, friendly birthday message for a client named {$clientName}.";
$payload = json_encode([
'model' => $model,
'messages' => [
['role' => 'system', 'content' => 'You are a creative assistant for a beauty spa.'],
['role' => 'user', 'content' => $prompt]
],
'temperature' => 0.7
]);
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $openaiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'OpenAI API error: ' . curl_error($ch) . "\n";
return 'Happy Birthday!';
}
curl_close($ch);
$data = json_decode($response, true);
if (isset($data['choices'][0]['message']['content'])) {
return trim($data['choices'][0]['message']['content']);
}
return 'Happy Birthday!';
}
?>