You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP Webbot对接Google Calendar指南:自动添加日程提醒方法

Connecting Your PHP Webbot to Google Calendar for Automated Reminders

Great job getting the email extraction part of your webbot up and running—you’re already halfway there! Let’s break down exactly how to automate adding those task, assignment, and exam reminders to Google Calendar using PHP.

Step 1: Set Up Google Calendar API Credentials

First, you need to get authorized to interact with Google’s API:

  • Go to the Google Cloud Console and create a new project (or use an existing one).
  • Search for "Google Calendar API" in the API library, enable it for your project.
  • Navigate to IAM & Admin > Service Accounts, create a new service account.
  • Once the service account is created, go to Keys > Add Key > Create New Key, select JSON as the format, and download the key file (keep this safe—it’s your bot’s access pass).
  • Finally, share your target Google Calendar with the service account’s email address (found in the JSON key file) and grant it "Editor" permissions so it can create events.

Step 2: Install the Google Client Library

Use Composer to install the official Google API client for PHP—this handles all the authentication and API communication heavy lifting:

composer require google/apiclient:^2.0

Step 3: Initialize the Google Calendar Service in PHP

Here’s how to load your credentials and set up the service instance:

require __DIR__ . '/vendor/autoload.php';

$client = new Google\Client();
$client->setApplicationName('My Assignment Reminder Webbot');
$client->setScopes([Google\Service\Calendar::CALENDAR_EVENTS]);
$client->setAuthConfig('path/to/your/service-account-key.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');

// Create the Calendar service object
$service = new Google\Service\Calendar($client);

Step 4: Convert Extracted Email Data to Calendar Events

Take the task/assignment details you extracted from emails and format them into a Google Calendar event structure. For example, if you have a submission date for an assignment:

// Example data from your email extraction
$taskTitle = "Math Homework #5";
$taskDescription = "Chapters 8-9 problems 1-10 due";
$submissionDateTime = new DateTime("2024-10-15 23:59:00", new DateTimeZone("America/New_York"));
$reminderHoursBefore = 24; // Send reminder 1 day before

// Format event for Google Calendar
$event = new Google\Service\Calendar\Event([
    'summary' => $taskTitle,
    'description' => $taskDescription,
    'start' => [
        'dateTime' => $submissionDateTime->format(DateTime::RFC3339),
        'timeZone' => "America/New_York",
    ],
    'end' => [
        // For a deadline, you can set end time to 1 hour after start (or same as start for all-day)
        'dateTime' => $submissionDateTime->modify('+1 hour')->format(DateTime::RFC3339),
        'timeZone' => "America/New_York",
    ],
    'reminders' => [
        'useDefault' => false,
        'overrides' => [
            ['method' => 'email', 'minutes' => $reminderHoursBefore * 60],
            ['method' => 'popup', 'minutes' => 60], // Optional popup reminder 1 hour before
        ],
    ],
]);

$calendarId = 'primary'; // Use 'primary' for the service account's calendar, or your personal calendar ID if shared
$event = $service->events->insert($calendarId, $event);

echo "Event created: " . $event->htmlLink;

Key Notes to Avoid Headaches

  • Time Zones: Always specify the time zone explicitly in both your DateTime objects and the event data—this prevents off-by-hour errors.
  • Calendar Permissions: Double-check that the service account’s email has Editor access to the calendar you’re trying to write to. If you get a "permission denied" error, this is usually the culprit.
  • Error Handling: Wrap the event insertion in a try/catch block to handle API errors (like invalid date formats or quota limits):
try {
    $event = $service->events->insert($calendarId, $event);
    echo "Success! Event added: " . $event->getId();
} catch (Exception $e) {
    echo "Error creating event: " . $e->getMessage();
}
  • All-Day Events: If your deadline is an all-day date (no specific time), use the date field instead of dateTime in the start/end arrays, e.g., 'date' => '2024-10-15'.

That’s it! With this setup, your PHP webbot can take the extracted task details and automatically push reminders to Google Calendar without any manual intervention.

内容的提问来源于stack exchange,提问作者Tanuj Thakkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:56:36