About Me

我的相片
台北市, Taiwan
我是方選,
方白科技(finebind tech.)共同創辦人,
臺大資管所畢,
希望能幫助更多的人!

FB: http://fb.com/function1122
LINE: http://bit.ly/1foeZft (手機開啟點擊網址自動加入)

最新15則讀者回應

最新文章

FUNction's 上課筆記

Label Cloud

Blog Archive

FeedBurner

追蹤者

[教學] 在PHP 中使用Google Calendar API - Part 4 新增事件(含提醒)

FUNction 於 2008年7月22日 晚上9:26 發表
大綱:透過API 將事件新增到Google Calendar 上,並設定發生前五分鐘用簡訊提醒
前言
久等了!最近小弟有點低潮,所以這篇拖了一段時間才發出。建立事件我覺得是個最有趣的環節,另外我特別企畫設定成讓Google 發簡訊到你的手機,提醒你事件要開始囉!

新增一筆事件
透過首頁建立事件的表單,將事件傳送到new.php 去建立。建立完成後顯示建立成功的訊息,並將剛剛建立的事件復誦一次。

在首頁建立表單
其實之前的範例包首頁的表單已經打好了,我這邊再做一次是給光是看文章學的人。請在index.php 的 <body>標籤下加入以下html 語法

<form action="new.php" method="post" name="GClab" id="GClab">
<h2>新增事件</h2>
<p>*標題
<input name="title" type="text" id="title" size="20">
<br>
內容
<textarea name="content" cols="60" rows="5" id="content"></textarea>
<br>
地點
<input name="where" type="text" id="where" size="20">
<br>
*開始日期
<input name="startDate" type="text" id="startDate" value="2008-07-13" size="10">
時間
<input name="startTime" type="text" id="startTime" value="17:55" size="6">
<br>
*結束日期
<input name="endDate" type="text" id="endDate" value="2008-07-14" size="10">
時間
<input name="endTime" type="text" id="endTime" value="03:55" size="6">
(請依照範例格式)<br>
<input type="submit" name="Submit" value="送出">
</form>
<hr />

這是一個表單,將事件送到new.php 去處理。

登入Google 服務&接變數
接著我們建立new.php 網頁,並先打入以下程式碼,我相信看系列文章的讀者可以知道一樣是登入Google ,然後我們用$_POST 去取得index.php POST 過來的變數。new.php 接收了事件的標題(title)、地點(where)、內容(content)、開始日期(startDate)、開始時間(startTime)、結束日期(endDate)、結束時間(endTime)等資訊。
<?php
include('config.inc.php');

$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // 提供Calendar 的服務名稱
$client = Zend_Gdata_ClientLogin::getHttpClient($googleAccount, $googlePassword, $service);
$gdataCal = new Zend_Gdata_Calendar($client);

//Title 和when 是必要的欄位,這邊做個防呆
if($_POST['title'] != ''&& $_POST['startDate'] != ''&& $_POST['startTime'] != ''&& $_POST['endDate'] != ''&& $_POST['endTime'] != ''){
$title = $_POST['title'];
$where = $_POST['where'];
$content = $_POST['content'];
$startDate = $_POST['startDate'];
$startTime = $_POST['startTime'];
$endDate = $_POST['endDate'];
$endTime = $_POST['endTime'];
}else{
die("[<a href=\"./\">back</a>]<hr /><b>Warning</b>: Too few arguments.");
}
?>

如果首頁傳過來的變數不足,就顯示Too few arguments. 的警告,die()讓程式不要繼續執行。值得注意的是這邊開始日期與開始時間是分成兩個變數去抓,但是等一下還是會透過程是去處理成一個Google 看的懂的格式(結束時間、日期亦同)。

建立單一事件
請在上述程式碼尾端?>前加入以下程式碼:
$calendarURL="http://www.google.com/calendar/feeds/".urlencode($calendarID)."/private/full";

$newEvent = $gdataCal->newEventEntry();
$newEvent->title = $gdataCal->newTitle($title);
$newEvent->where = array($gdataCal->newWhere($where));
$newEvent->content = $gdataCal->newContent($content);

$when = $gdataCal->newWhen();
$when->startTime = "{$startDate}T{$startTime}:00.000+08:00";
$when->endTime = "{$endDate}T{$endTime}:00.000+08:00";
$newEvent->when = array($when);

//當上傳事件後,Google 會回傳相同的事件,我們就可以得知這個事件的ID 了
//這裡值得注意的是第二個參數,指定上傳到我第二本日曆
$createdEvent = $gdataCal->insertEvent($newEvent, $calendarURL);

一開始做了一個變數$calendarURL 是因為我想要把事件加入到我第二本日曆,如果沒指定則會預設新增道第一本去。接下來我們就使用他寫好的方法將事件建立成物件,比較需要注意的是startTime 與endTime 的格式,尾端的+08:00 是因為我位在台北,是+8 時區,如果沒有寫時區會亂掉唷。

最後使用insertEvent() 建立事件,第二個參數$calendarURL 是指定新增道的日曆,不寫就會新增到預設日曆。當建立完成後,Google 會把事件回傳到$createdEvent 裡面,所以我們可以用$createdEvent->id->text 去找出事件建立完成後的ID。

建立簡訊提醒
簡訊提醒我故意透過修改事件的方法去新增,這樣也可以體會一下修改的方法。要使用簡訊提醒必須要先讓手機通過Google 的認證,這裡可以參考Google日曆也能寄簡訊通知你的行程事項(SMS) 香腸炒魷魚 這篇文章設定手機,在此就不贅述。請在?>前加入以下程式:
//到這裡已經完成事件的建立了,接著騷包一點讓他來個簡訊提醒吧
//目前網路上的範例提醒不能用,因為他把reminders 打成reminder 了
$query = $gdataCal->newEventQuery();
$query->setUser($calendarID);
$query->setVisibility('private');
$query->setProjection('full');
$query->setEvent($createdEvent->id->text);
$event = $gdataCal->getCalendarEventEntry($query);
$times = $event->when;
foreach ($times as $when) {
$reminder = $gdataCal->newReminder();
$reminder->setMinutes(5); //五分鐘前提醒我
$reminder->setMethod('sms'); //用簡訊
$when->reminders = array($reminder);
}
$eventNew = $event->save();

這裡要注意的是就連Zend 官網範例與Google 的PHP 範例都把$when->reminders = array($reminder);寫成$when->reminder = array($reminder); 所以範例會有問題。另外提醒除了sms(簡訊)之外,還有email 與alert 。email 會將提醒寄到你的信箱;alert 則是在Google Calendar 上彈出一個javascript 的alert()。最後,這裡使用$event->save();將修改後的事件儲存。

建立網頁顯示
?>後面加入以下html 語法,顯示事件建立成功的訊息,並使用建立成功事件回傳的$createdEvent 顯示建立事件的細節,告知使用者剛剛完成的建立。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>FUNction's Lab 讀取Google 行事曆 :: <?=$createdEvent->title->text?></title>
</head>
<body>
[<a href="./">上一頁</a>] [<a href="delete.php?id=<?=basename($createdEvent->id->text)?>">刪除</a>]
<hr />
<h1><font color="red">事件建立成功!</font></h1>
<?php
echo "<h2>" . $createdEvent->title->text . "</h2>\n";
echo "<ul>\n";
echo "\t<li><b>內容:</b>".$createdEvent->content->text."</li>\n";
foreach ($createdEvent->where as $where) {
echo "\t<li><b>地點:</b>" . $where->valueString . "</li>\n";//地點
}
foreach ($createdEvent->when as $when) {
echo "\t<li><b>開始時間:</b>" . $when->startTime . "</li>\n";
echo "\t<li><b>結束時間:</b>" . $when->endTime . "</li>\n";
}
echo "\t<li><b>事件ID:</b>".basename($createdEvent->id->text)."</li>\n";
echo "</ul>\n";
?>
</body>
</html>
這邊顯示的方法跟上一篇顯示單一事件如出一轍,我想應該很好明白。

結語
我想今天應該還蠻讓人興奮的,做到這裡已經幾乎可以使用了,只剩下刪除事件。刪除事件是我的最後一篇了,我打算在那篇列出我所參考的資料,希望對持續開法的人能有拋磚引玉的功效。

範例程式下載:GClab-part-4.zip

上一篇[教學] 在PHP 中使用Google Calendar API - Part 3 檢視單一事件
下一篇[教學] 在PHP 中使用Google Calendar API - END 刪除事件(含補充) Tags: , , ,

讀者回應 ( 4 意見 )

不好意思,想請問您在編寫此api的時候,有無碰到加入單一事件後發生error(Expected response code 200, got 400)的情況?另外感謝您這系列的詳細解說,這個blog對google api的介紹讓我對它有進一步的認識感謝:)

XD 找到答案了,自己回答
try and catch可以用以下語法知道錯誤到底是什麼
echo $e->getResponse()->getRawBody();

我碰到的原因是datetime的格式錯誤,
時間的輸入值必須是xx:xx,
小時或分不足兩位數要補零。

您好
當頁面跳轉到new.php時出現以下錯誤
雖出現錯誤
但的確新增的那些資訊會直接真的紀錄到我的日曆上面
請問這大概是什麼問題呢

Fatal error: Uncaught exception 'Zend_Gdata_App_Exception' with message 'No root element' in /home/littleba/public_html/GClab/library/Zend/Gdata/App/Base.php:313 Stack trace: #0 /home/littleba/public_html/GClab/library/Zend/Gdata/App.php(832): Zend_Gdata_App_Base->transferFromXML('importUrl('https://www.goo...', 'Zend_Gdata_Cale...', NULL) #3 /home/littleba/public_html/GClab/library/Zend/Gdata.php(187): Zend_Gdata_App->getEntry('https://www.goo...', 'Zend_Gdata_Cale...') #4 /home/littleba/public_html/GClab/library/Zend/Gdata/Calendar.php(126): Zend_Gdata->getEntry('https://www.goo...', 'Zend_Gdata_Cale...') #5 /home/littleba/public_html/GClab/new.php(44): Zend_Gdata_Calendar->getCalendarEventEntry(Object(Zend_Gdata_Calendar_EventQuery)) #6 {main} thrown in /home/littleba/public_html/GClab/library/Zend/Gdata/App/Base.php on line 313

我的也是跟小包出現一樣的訊息,請問有解嗎?

張貼留言

如果沒有帳戶,建議使用「名稱/網址」留言喔^^