使用PHP设置Google日历活动的提醒

时间:2012-03-26 03:56:57

标签: php google-calendar-api reminders

我正在开发一个网站,使用代码from this blog post将事件添加到Google日历。

现在我想为事件发生前15分钟设置的每个事件设置提醒。

任何人都可以就如何实现这个目标给我一些指导吗?

1 个答案:

答案 0 :(得分:2)

创建活动时设置提醒非常简单。您只需在<gd:when></gd:when>标记中添加几行。

<gd:when startTime='2006-03-30T22:00:00.000Z' endTime='2006-03-30T23:00:00.000Z'>
  <gd:reminder minutes='15' method='email' />
  <gd:reminder minutes='15' method='alert' />
</gd:when>

以下是一种更新的addEvent()方法,其中包括对提醒的支持:

public function addEvent($params) {  
    $url = "http://www.google.com/calendar/feeds/{$this->getFeedEmail()}/private/full";  

    //startTime should be a time() value so we can convert it into the correct format  
    $params["startTime"] = date("c", $params["startTime"]);  

    //If no end-time is specified, set the end-time to 1 hour after the start-time  
    if(!array_key_exists("endTime", $params)) {  
        $params["endTime"] = date("c", strtotime($params["startTime"])+60*60*1);  
    }  

    $reminders = '';
    if(is_array($params['reminders'])) {
        foreach($params['reminders'] as $rem) {
            $reminders .= "<gd:reminder minutes='{$rem['minutes']}' method='{$rem['method']}' />"\n; 
        }
    }

    $xml = "<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005">  
              <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"></category>  
              <title type="text">{$params["title"]}</title> 
              <content type="text">{$params["content"]}</content> 
              <gd:transparency value="http://schemas.google.com/g/2005#event.opaque">  
              </gd:transparency>  
              <gd:eventstatus value="http://schemas.google.com/g/2005#event.confirmed">  
              </gd:eventstatus>  
              <gd:where valuestring="{$params["where"]}"></gd:where> 
              <gd:when starttime="{$params["startTime"]}" endtime="{$params["endTime"]}">
                {$reminders} 
              </gd:when> 
            </entry>";  

    //Do the initial POST to Google  
    $ret = $this->calPostRequest($url, $xml);  

    //If Google sends back a gsessionid, we need to make the request again  
    $matches = array();  
    if(preg_match('/gsessionid=(.*?)\s+/', $ret, $matches)) {  
        $url .= "?gsessionid={$matches[1]}";  
        $ret = $this->calPostRequest($url, $xml);  
    }  

    //Parse the XML response (which contains the newly added entry)  
    $retFields = explode("\n", $ret);  
    //print_r($retFields);  
    $entryXML = simplexml_load_string($retFields[count($retFields)-1]);  

    //Return an array containing the entry id (url) and the etag  
    return array(  
            "id"=> (string)$entryXML->id,  
            "etag"=> $this->getETagFromHeader($retFields),  
            "link"=> $this->getEditLinkFromHeader($retFields)  
            );  
}  

你会这样称呼它:

$entryData = $cal->addEvent(array(  
    "title"=> "Auto Test event",  
    "content"=> "This is a test event",  
    "where"=> "Test location",  
    "startTime"=> time()+60*60*24*1,
    "reminders"=> array(
        array("method"=>"email", "minutes"=>"15"),
        array("method"=>"alert", "minutes"=>"15"),
    ) 
));