【问题标题】:Google Calendar API - PHP - set event's colorGoogle Calendar API - PHP - 设置事件的颜色
【发布时间】:2018-11-01 18:19:23
【问题描述】:

我们如何在使用 Google Calendar API (PHP) 插入事件时设置颜色?

【问题讨论】:

    标签: php google-calendar-api


    【解决方案1】:

    这是我编写的一个 PHP 函数和我提取的 PHP 客户端库的一部分,以允许我在日历中创建事件、删除事件等,但代码只是为了向您展示 colorId 需要设置的位置以及使它工作的语法。

    Google 将为您可以设置 colorId 的前 11 个整数中的每一个显示颜色。

    1 蓝色

    2个绿色

    3 紫色

    4 红色

    5 黄色

    6 橙色

    7 绿松石

    8 灰色

    9 粗体蓝色

    10 粗体绿色

    11 粗体红色

    function calendar_update($heading,$details,$address,$calendar_name,$start_time,$end_time,$event_id = "")
    {
        $client = get_google_client($calendar_name); // ID of your Google calendar
        $capi = new GoogleCalendarApi($client);
    
        $event['event_time']['start_time'] = $start_time; // Start time of event
        $event['event_time']['end_time'] = $end_time;     // End time of the event
        $event['summary'] = $heading;                     // The title of the event
        $event['location'] = $address;                    // Address field of event
        $event['description'] = $details;                 // Body of the event
        $event['colorId'] = 3;                            // colorId of event (see above)
        $event['attendees'] = $calendar_name;
    
        try 
        {
            $event_id = $capi->CreateCalendarEvent($calendar_name, $event, 0,
                 $event['event_time'], "Australia/Sydney", $_SESSION['access_token']);
        }
        catch(Exception $e) 
        {
            $result = $e->getMessage();
            return("ERROR\t$e");
        }
        return($event_id);
    }
    
    
    
    class GoogleCalendarApi
    {
        public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {    
            $url = 'https://accounts.google.com/o/oauth2/token';            
    
            $curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
            $ch = curl_init();        
            curl_setopt($ch, CURLOPT_URL, $url);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
            curl_setopt($ch, CURLOPT_POST, 1);        
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);    
            $data = json_decode(curl_exec($ch), true);
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);        
            if($http_code != 200) 
                throw new Exception('Error : Failed to receieve access token');
    
            return $data;
        }
    
        public function GetUserCalendarTimezone($access_token) {
            $url_settings = 'https://www.googleapis.com/calendar/v3/users/me/settings/timezone';
    
            $ch = curl_init();        
            curl_setopt($ch, CURLOPT_URL, $url_settings);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));    
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
            $data = json_decode(curl_exec($ch), true); //echo '<pre>';print_r($data);echo '</pre>';
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);        
            if($http_code != 200) 
                throw new Exception('Error : Failed to get timezone');
    
            return $data['value'];
        }
    
        public function GetCalendarsList($access_token) {
            $url_parameters = array();
    
            $url_parameters['fields'] = 'items(id,summary,timeZone)';
            $url_parameters['minAccessRole'] = 'owner';
    
            $url_calendars = 'https://www.googleapis.com/calendar/v3/users/me/calendarList?'. http_build_query($url_parameters);
    
            $ch = curl_init();        
            curl_setopt($ch, CURLOPT_URL, $url_calendars);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));    
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
            $data = json_decode(curl_exec($ch), true); //echo '<pre>';print_r($data);echo '</pre>';
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);        
            if($http_code != 200) 
                throw new Exception('Error : Failed to get calendars list');
    
            return $data['items'];
        }
    
        public function CreateCalendarEvent($calendar_id, $summary, $all_day, $event_time, $event_timezone, $access_token) {
            $url_events = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events';
    
            $curlPost = $summary;
    //        $curlPost = array('summary' => $summary);
            if($all_day == 1) {
                $curlPost['start'] = array('date' => $event_time['event_date']);
                $curlPost['end'] = array('date' => $event_time['event_date']);
            }
            else {
                $curlPost['start'] = array('dateTime' => $event_time['start_time'], 'timeZone' => $event_timezone);
                $curlPost['end'] = array('dateTime' => $event_time['end_time'], 'timeZone' => $event_timezone);
            }
            $ch = curl_init();        
            curl_setopt($ch, CURLOPT_URL, $url_events);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
            curl_setopt($ch, CURLOPT_POST, 1);        
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token, 'Content-Type: application/json'));    
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost));    
            $data = json_decode(curl_exec($ch), true);
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);        
            if($http_code != 200) 
                throw new Exception('Error : Failed to create event');
    
            return $data['id'];
        }
    
        public function UpdateCalendarEvent($event_id, $calendar_id, $summary, $all_day, $event_time, $event_timezone, $access_token) {
            $url_events = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events/' . $event_id;
    
            $curlPost = $summary;
    //        $curlPost = array('summary' => $summary);
            if($all_day == 1) {
                $curlPost['start'] = array('date' => $event_time['event_date']);
                $curlPost['end'] = array('date' => $event_time['event_date']);
            }
            else {
                $curlPost['start'] = array('dateTime' => $event_time['start_time'], 'timeZone' => $event_timezone);
                $curlPost['end'] = array('dateTime' => $event_time['end_time'], 'timeZone' => $event_timezone);
            }
            $ch = curl_init();        
            curl_setopt($ch, CURLOPT_URL, $url_events);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');        
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token, 'Content-Type: application/json'));    
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost));    
            $data = json_decode(curl_exec($ch), true);
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);        
            if($http_code != 200) 
                throw new Exception('Error : Failed to update event Code: ' . $http_code);
        }
    
        public function DeleteCalendarEvent($event_id, $calendar_id, $access_token) {
            $url_events = 'https://www.googleapis.com/calendar/v3/calendars/' . $calendar_id . '/events/' . $event_id;
    
            $ch = curl_init();        
            curl_setopt($ch, CURLOPT_URL, $url_events);        
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');        
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token, 'Content-Type: application/json'));        
            $data = json_decode(curl_exec($ch), true);
            $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
            if($http_code != 204) 
                throw new Exception('Error : Failed to delete event');
        }
    }
    

    【讨论】:

      【解决方案2】:

      creating an event 时可以设置可选的colorId 属性。您可以从colors endpoint 检索颜色列表。

      【讨论】:

      【解决方案3】:

      在这里,您可以找到要为事件插入的颜色的每个 idColor Google Calendar id Color for events

      【讨论】:

      • 链接返回 404
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多