Search code examples
phpgoogle-calendar-apireminders

Set a reminder for a Google Calendar event using PHP


I am developing a site to add events to a Google Calendar using code from this blog post.

Now I want to set a reminder for each event that is set 15 minutes before the event.

Can anyone give me some direction on how to achieve this?


Solution

  • Setting a reminder when creating an event is very easy. You just have to add a couple additional lines to the <gd:when></gd:when> tag.

    <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>
    

    Here's an updated addEvent() method that includes support for reminders:

    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)  
                );  
    }  
    

    And you would call it like this:

    $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"),
        ) 
    ));