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

Codeigniter框架下Full Calendar事件同步Google Calendar报错求助

问题排查与解决方案

核心错误原因:你提供的代码中未初始化Google_Client客户端和Google_Service_Calendar服务实例,直接调用了未定义的$service变量触发报错。

前置检查

  • 确认CodeIgniter已加载composer自动加载文件:CodeIgniter 3 需在入口index.php中添加require_once './vendor/autoload.php';,CodeIgniter 4 默认开启composer依赖加载无需额外配置。
  • 已完成Google Cloud基础配置:
    • 启用Google Calendar API
    • 创建服务账号凭据并下载JSON格式的密钥文件
    • 目标日历cicuser3@gmail.com已向服务账号邮箱授予编辑权限

修正后的控制器代码

function google_event(){
    // 初始化Google客户端
    $client = new Google_Client();
    $client->setApplicationName('你的自定义应用名称');
    // 替换为你的凭据文件实际存储路径
    $client->setAuthConfig(APPPATH . 'config/google-calendar-cred.json');
    $client->addScope(Google_Service_Calendar::CALENDAR);
    $client->setAccessType('offline');

    // 初始化日历服务实例,解决$service未定义问题
    $service = new Google_Service_Calendar($client);

    // 事件构造逻辑保留,后续可替换为FullCalendar前端提交的动态参数
    $event = new Google_Service_Calendar_Event(array(
        'summary' => 'Google I/O 2015',
        'location' => '800 Howard St., San Francisco, CA 94103',
        'description' => 'A chance to hear more about Google\'s developer products.',
        'start' => array(
          'dateTime' => '2021-09-28T09:00:00-07:00',
          'timeZone' => 'America/Los_Angeles',
        ),
        'end' => array(
          'dateTime' => '2021-09-28T17:00:00-07:00',
          'timeZone' => 'America/Los_Angeles',
        ),
        'recurrence' => array(
          'RRULE:FREQ=DAILY;COUNT=2'
        ),
        'attendees' => array(
          array('email' => 'lpage@example.com'),
          array('email' => 'sbrin@example.com'),
        ),
        'reminders' => array(
          'useDefault' => FALSE,
          'overrides' => array(
            array('method' => 'email', 'minutes' => 24 * 60),
            array('method' => 'popup', 'minutes' => 10),
          ),
        ),
    ));
    
    $calendarId = 'cicuser3@gmail.com';
    $event = $service->events->insert($calendarId, $event);
    printf('Event created: %s\n', $event->htmlLink);
}

FullCalendar对接说明

功能跑通后,只需将事件构造部分的固定值替换为前端FullCalendar提交的动态事件参数,即可实现新增事件自动同步到谷歌日历的需求。

内容的提问来源于stack exchange,提问作者Vinhaar iqbal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 13:27:03