跳到主要內容

如何使用Google Calendar API in PHP(1)

http://framework.zend.com/download/webservices
首先到此下載函式庫,解壓縮開來的資料夾內有
demos
documentation
library
test
其中demos可以從裡面看一些範例。

如果只是要用google api那就只要將library拷貝出來到你的資料夾底下就可以

而底下的範例要感謝 FUNction's 無趣隨筆分享的此篇文章

裡面寫到了使用的方法

在這邊借用一下

建立config.inc.php 檔案內容如下


<?php
//以下欄位請依照自己需要調整
$googleAccount = 'user@gmail.com'; //Google 帳號
$googlePassword = 'pwd'; //Google 密碼
$calendarID = 'user@gmail.com';
//$calendarID = 'fr53rl3ot0kn65duem7m9gc9og@group.calendar.google.com'; //Google Calendar 的 ID


//這裡透過程式碼動態修改php.ini 的include_path 值,告訴系統Zend Gdata Library 的位置
$slash = (strstr(ini_get('extension_dir'), '/'))?"/":"\\"; //Windows 與Unix 的斜線方向不同,需要考慮到
$includePath = dirname(__FILE__).$slash.'library';
ini_set('include_path', $includePath); //動態設定php.ini

//這邊是在設定程式把Zend Gdata Library 載入程式碼中
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_Calendar');
?>


還要建立index.php來顯示日曆底下的事件簿

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

//接下來這邊就是讀取某本日曆的方法,據說最多只能列出25筆資料
$query = $gdataCal->newEventQuery();
//原始範例中setUser給的參數是'default' 這樣的話是開啟範例中$client的主日曆
//但由網頁說明中我們可指定User 來開啟任何一本日曆,例如這邊我設定成第二本日曆的ID
$query->setUser($calendarID);
$query->setVisibility('private');
$query->setProjection('full');
$query->setOrderby('starttime');
$eventFeed = $gdataCal->getCalendarEventFeed($query);

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>FUNction's Lab 讀取Google 行事曆</title>
</head>
<body>
<?php
foreach ($eventFeed as $event) {
echo "<h2><a href=\"event.php?id=".basename($event->id->text)."\">" . $event->title->text . "</a></h2>\n";
echo "<ul>\n";
echo "\t<li><b>內容:</b>".$event->content->text."</li>\n";
foreach ($event->where as $where) {
echo "\t<li><b>地點:</b>" . $where->valueString . "</li>\n";
}
echo "</ul>\n";
}
?>
</body>
</html>



大家如果是Windows平台的話,記得要到php.ini中開啟ssl,不然會無法登入google的服務喔。
方法如下:
1.開啟php.in
2.搜尋extension=php_openssl.dll
3.將前面的分號除掉
4.完成
現在大家知道如何使用Google Calendar API並沒有想像中有難度,其他的教學待續

PS. Google Calendar的ID必需要登入到日曆底下才可以看到

留言

這個網誌中的熱門文章

[CSS] z-index 在不同瀏覽器繼承問題

今天會討論到這個課題,是因為要實做一個Popup dialog,所以我們希望的結果如下圖。 可是在IE7 卻發生了這樣的情況。 Popup不論怎麼設定z-index都無法浮在最上層,我們看一下html架構發生什麼事情。

[教學] 快快樂樂刪除CodeIgniter index.php

預設的CI網址預設都設定為index.php同一層級,因此所有的程式都必須指定index.php導向才能開始,例如 http://localhost/ci/index.php/welcome/test http://localhost/ci/welcome/test 本文將說明如何將惱人的index.php消除,還你一個漂亮的URL。 設定開始: 接下來說明如何使用rewrite方式將惱人的index.php去除。 rewrite不清楚的人,煩請先自行google 首先要先確定Apache的 mod_rewrite 有 開啟 ,如果沒有開啟請設定好之後重新啟動apache。 接著,在根目錄底下建立一個新檔案,檔名為 .htaccess ,裡面程式碼如下: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> 接著到 application/config/config.php ,開啟檔案修改 $config['index_page'] = ""; 注意: /index.php/$1 要根據你目錄,例如 http://localhost/index.php ,網站根目錄為 /ci/index.php 則要寫成 /ci/index.php/$1 接著至CI目錄下,尋找 config\config.php , 修改一下裡面的檔案,修改如下: $config['index_page'] = ""; 存檔後,如此一來大功告成。 參考資料 官方網站說明

[教學] Mojito 安裝與入門,install mojito for beginner.

mojito ,最近終於從 YDN 對外公開此專案,這個套件主要用於解決前端多重裝置及瀏覽器端的問題,後端服務採取 node.js ,因此使用上必定要先準備以下幾個元素 準備素材 c++ complier git Node.js > 0.4.x NPM > 1.0.x 安裝方式 git clone git://github.com/yahoo/mojito.git cd mojito/source sudo npm install -g . npm install . 以上四個簡單的步驟,就可以把 mojito module 完整安裝到服務器上,接著就可以開始進入 mojito 的世界 使用方式 mojito 提供了完整的 command line 給開發者使用,接著先建立一個基本 hello world 專案,跟著以下步驟完成第一個專案。首先建立一個 mojito application, mojito create app hello cd hello 切換到目錄之後,再接著建立自己的 mojit,這邊的 mojit 就像是一個應用(application)可能會包含許多個獨立網站體,擁有獨立架構的 MVC ,包含內部設定等,詳細資料可以參考官方的 說明 ,建立 mojit mojito create mojit HelloMojit 輸入指令後,會看到顯示結果如下, creating mojit called 'HelloMojit' (using "default" archetype) ✔ mojit: HelloMojit created! ✔ mojito done. 接著修改 application.json 這個 mojit 設定檔案,讓剛才新建立的 hellp application 指定底下有一個 mojit -> HelloMojit ,讓應用可以去執行 mojit controller ,修改如下, [ { "settings": [ "master" ], "appPort": 8666, "specs": { ...