Using php to send Google Calendar SMS

First of all, you need to install Zend Gdata on your web server. Using following codes. Just modified at 12, 13 and 105 lines to your own. Make sure your username and password is safe. You better don’t write your password in your php codes. You can design your input form. And using https to send your password to your web server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?
/**
 * Author : Sam Tseng
 * Date : 18-MAY-2009
 *
 * Program Name : Accessing Google Calendar Demo
 * Function : Using php to add a Google Calendar event and a reminder.
 *
*/
 
// Please use your username and password at your google service.
$username = "username@gmail.com";
$password = "xxxxxxxxxxxxxxxxxxxxxx";
 
$alarm_method = "";
 
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
 
function getClientLoginHttpClient($user, $pass) {
  $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
 
  $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  return $client;
}
 
function createEvent($client, $title, $desc, $where,
                     $startDate, $startTime, $endDate, $endTime, $tzOffset = '+08') {
 
  $gc = new Zend_Gdata_Calendar($client);
  $newEntry = $gc->newEventEntry();
  $newEntry->title = $gc->newTitle(trim($title));
  $newEntry->where  = array($gc->newWhere($where));
 
  $newEntry->content = $gc->newContent($desc);
  $newEntry->content->type = 'text';
 
  $when = $gc->newWhen();
  $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
  $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
  $newEntry->when = array($when);
 
  $createdEntry = $gc->insertEvent($newEntry);
  return $createdEntry->id->text;
}
 
function setReminder($client, $eventId, $minutes=15) {
  $gc = new Zend_Gdata_Calendar($client);
  global $alarm_method;
  $method = $alarm_method;
  if ($event = getEvent($client, $eventId)) {
      $times = $event->when;
      foreach ($times as $when) {
          $reminder = $gc->newReminder();
          $reminder->setMinutes($minutes);
          $reminder->setMethod($method);
          $when->reminders = array($reminder);
      }
      $eventNew = $event->save();
      return $eventNew;
  } else {
      return null;
  }
}
 
function getEvent($client, $eventId) {
  $gdataCal = new Zend_Gdata_Calendar($client);
  $query = $gdataCal->newEventQuery();
  $query->setUser('default');
  $query->setVisibility('private');
  $query->setProjection('full');
  $query->setEvent($eventId);
 
  try {
      $eventEntry = $gdataCal->getCalendarEventEntry($query);
      return $eventEntry;
  } catch (Zend_Gdata_App_Exception $e) {
      var_dump($e);
      return null;
  }
}
 
function triger_event_now($method, $title, $desc, $where) {
  global $username, $password, $alarm_method;
  $alarm_method = $method;
  $client = getClientLoginHttpClient($username, $password);
  $today = date("Y-m-d");
  $now = date("H:i", mktime(date("H"), date("i") + 12, date("s"),
date("m"), date("d"), date("Y")));
  // by default, I use Taiwan timezone. You should modify your own timezone.
  $id = createEvent($client, $title, $desc,
$where, $today, $now, $today, $now, "+08");
  $id = str_replace("http://www.google.com/calendar/feeds/default/private/full/",
"", $id);
  setReminder($client, $id, 10);
}
 
// There are three type of reminders which are "sms", "email" and "alert".
// The below is using sms to send the event to your mobile. This program will delay two minutes.
triger_event_now("sms", "surf The Paradiso blog", "http://www.samtseng.liho.tw/~samtz/blog/", "Anywhere");
?>

使用 php 傳送 Google Calendar 簡訊

首先,你需要在你的網頁伺服器安裝 Zend Gdata. 更改下列程式碼 :

改成你的 Google 帳號與密碼. 你要確保這個檔案不會被其他人讀取到. 最好不要把密碼寫在程式內. 你可以設計你的表單使用 https 傳遞你的密碼到你的網頁伺服器.

11
12
13
// Please use your username and password at your google service.
$username = "username@gmail.com";
$password = "xxxxxxxxxxxxxxxxxxxxxx";

觸發事件有三種方式 “sms”, “email” and “alert”. 下面展示是用 sms 提醒. 兩分鐘後簡訊就會傳送到你的手機.

102
103
104
// There are three type of reminders which are "sms", "email" and "alert".
// The below is using sms to send the event to your mobile. This program will delay two minutes.
triger_event_now("sms", "surf The Paradiso blog", "http://www.samtseng.liho.tw/~samtz/blog/", "Anywhere");

如果有問題歡迎留言一起討論.