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

如何在PHP中通过Google Calendar API删除事件并发送通知

如何在Google Calendar API删除事件时设置sendNotifications参数

我来帮你搞定这个问题!当调用Google Calendar API删除事件时,sendNotifications参数是作为URL查询参数来传递的,而不是放在请求体或者curl的其他选项里,这也是很多开发者容易踩坑的点。

先搞懂参数作用

sendNotifications是一个布尔值参数:

  • 设置为true:删除事件时会自动给所有事件参与者发送通知邮件
  • 设置为false(默认值):删除时不发送任何通知

修改你的代码实现

你只需要把这个参数直接拼在请求URL的末尾即可,同时记得对calendar_id和event_id做URL编码,避免特殊字符导致请求失败。这里给你调整后的完整函数示例:

public function DeleteCalendarEvent($event_id, $calendar_id, $access_token, $sendNotifications = false) {
    // 拼接带参数的请求URL,同时对ID做URL编码
    $url_events = 'https://www.googleapis.com/calendar/v3/calendars/' 
        . urlencode($calendar_id) 
        . '/events/' 
        . urlencode($event_id) 
        . '?sendNotifications=' . ($sendNotifications ? 'true' : 'false');
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url_events);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    
    // 必须添加Authorization请求头,否则API会返回权限错误
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $access_token,
        'Content-Type: application/json'
    ]);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    // 处理curl请求错误
    if (curl_errno($ch)) {
        $error = curl_error($ch);
        curl_close($ch);
        return ['error' => $error];
    }
    
    curl_close($ch);
    
    return [
        'http_code' => $httpCode,
        'response' => json_decode($response, true)
    ];
}

调用示例

如果需要发送删除通知,调用时传入true即可:

// 发送通知的删除请求
$result = $yourClassInstance->DeleteCalendarEvent('your-event-id', 'your-calendar-id', 'your-access-token', true);

// 不发送通知的删除请求(使用默认值)
$result = $yourClassInstance->DeleteCalendarEvent('your-event-id', 'your-calendar-id', 'your-access-token');

内容的提问来源于stack exchange,提问作者Muhammad Khairi Bin Salleh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:54:48