首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

在 PHP 应用程序中集成 Google Calendar(1)

在 PHP 应用程序中集成 Google Calendar(1)

简介很长时间以来,我一直使用的个人计划工具是 PalmPilot 自带的日程表工具。但在过去两年中,我逐渐转向了 Google Calendar:不仅仅因为它更具备 Web 特性,而且更容易共享事件信息、组织预约和处理不同类型的事件。
常用的缩写词
  • API:应用程序编程接口(Application Programming Interface)
  • HTTP:超文本传输协议(Hypertext Transfer Protocol)
  • PHP:超文本预处理器(Hypertext Preprocessor)
  • REST:具象状态传输(Representational state transfer)
  • RSS:真正简单的连锁(Really Simple Syndication)
  • URL:统一资源定位符(Uniform Resource Locator)
  • XML:可扩展标记语言

作为一名开发人员,我发现 Google Calendar 非常便于非正式的谈话:开发人员利用其 Data API 很容易对公共和用户私有日程表中存储的数据创建新的应用程序。这种 API 采用了 REST 模型,可通过任何支持 XML 的开发工具包访问,并且为很多常用的编程语言提供了客户机上的库,包括我钟爱的 PHP。
本文将介绍 Google Calendar Data API,说明如何在自定义的 PHP 应用程序中集成和使用日程表数据。本文通过例子说明了如何:
  • 从用户公共记事本中检索事件
  • 添加新的事件
  • 修改和删除事件
  • 按照关键字或日期范围搜索事件
我们开始吧!
理解 Calendar Data API编写 PHP 代码之前,首先简单了解一下 Google Calendar Data API。和所有基于 REST 的服务一样,这种 API 也接受包含一个或多个 XML 编码参数的 HTTP 请求,返回任何 XML 感知客户机都能解析的 XML 编码响应。对于 Google Calendar Data API,响应通常是一个包含请求信息的 Atom 或者 RSS 提要。
典型的 Google Calendar 提要包含大量的信息,足以创建有用的相关应用程序。我们来看一个例子,登录到 Google Calendar 帐户然后导航到日程表设置,找到您个人的日程表私有地址 URL。这个应该保密的 URL 提供了到日程表提要的只读访问,不需要首次授权,形式为 http://www.google.com/calendar/feeds/userid/private-magicCookie/basic。将该 URL 粘贴到 Web 浏览器(或者通过 HTTP 客户机发送 GET 来请求该提要),得到的结果如  所示:
清单 1. Google Calendar 提要示例
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
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'
xmlnspenSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:batch='http://schemas.google.com/gdata/batch'
xmlns:gCal='http://schemas.google.com/gCal/2005'
xmlns:gd='http://schemas.google.com/g/2005'>
  <id>http://www.google.com/calendar/feeds/user@gmail.com/
   private-cookie/basic</id>
  <updated>2008-06-13T19:15:18.000Z</updated>
  <category scheme='http://schemas.google.com/g/2005#kind'
  term='http://schemas.google.com/g/2005#event'/>
  <title type='text'>Joe User</title>
  <subtitle type='text'>Joe User</subtitle>
  <link rel='alternate' type='text/html'
  href='http://www.google.com/calendar/embed?src=user@gmail.com'/>
  <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml'
   href='http://www.google.com/calendar/feeds/user@gmail.com/private-cookie/basic'/>
  <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml'
   href='http://www.google.com/calendar/feeds/user@gmail.com/private-cookie/basic/batch'
    />
  <link rel='self' type='application/atom+xml'
  href='http://www.google.com/calendar/feeds/user@gmail.com/private-cookie/basic?
  max-results=25'/>
  <author>
    <name>Joe User</name>
    <email>user@gmail.com</email>
  </author>
  <generator version='1.0' uri='http://www.google.com/calendar'
  >Google Calendar</generator>
  <openSearch:totalResults>4</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
  <gCal:timezone value='Asia/Calcutta'/>
  <entry>
    <id>http://www.google.com/calendar/feeds/user@gmail.com/
    private-cookie/basic/xxxxxxxx</id>
    <published>2008-06-12T08:49:38.000Z</published>
    <updated>2008-06-13T19:06:21.000Z</updated>
    <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/g/2005#event'/>
    <title type='html'>Swim party</title>
    <summary type='html'>When: Sat Jun 21, 2008 12pm to 3:30pm&nbsp;
IST<br>
<br>Event Status: confirmed</summary>
    <content type='html'>When: Sat Jun 21, 2008 12pm to 3:30pm
IST<br />
<br />Event Status: confirmed</content>
    <link rel='alternate' type='text/html'
    href='http://www.google.com/calendar/event?eid=cGxwbHExOYHHDlOHQ4ZjA
    yMGMgdmlrcmFtLm1lbG9uZmlyZUBnb29nxmNvbQ' title='alternate'/>
    <link rel='self' type='application/atom+xml'
    href='http://www.google.com/calendar/feeds/user@gmail.com/
    private-cookie/basic/ddddddddddddddd/>
    <author>
      <name>Joe User</name>
      <email>user@gmail.com</email>
    </author>
  </entry>
  <entry>
    <id>http://www.google.com/calendar/feeds/user@gmail.com/
    private-cookie/basic/yyyyyyyyyyyyyyyy</id>
    <published>2008-06-12T08:48:30.000Z</published>
    <updated>2008-06-12T08:48:46.000Z</updated>
    <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/g/2005#event'/>
    <title type='html'>Dinner with the gang</title>
    <summary type='html'>When: Wed Jun 11, 2008 7pm to 9:30pm&nbsp;
IST<br>
<br>Event Status: confirmed</summary>
    <content type='html'>When: Wed Jun 11, 2008 7pm to 9:30pm
IST<br />
<br />Event Status: confirmed</content>
    <link rel='alternate' type='text/html'
    href='http://www.google.com/calendar/event?eid=
    MmhpYmV2cmowMM2kam9lZDQgdcmFtLm1lbG9uZmlyZU4858Bnb29nbGVtYWlsLmNvbQ'
    title='alternate'/>
    <link rel='self' type='application/atom+xml'
    href='http://www.google.com/calendar/feeds/user@gmail.com/private-cookie/
    basic/hhhhhhhhhhhhhhhhh'/>
    <author>
      <name>Joe User</name>
      <email>user@gmail.com</email>
    </author>
  </entry>
  <entry>
  ...
  </entry>
</feed>




所有的 Calendar 提要都以 <feed> 作为根元素。<feed> 元素中的 <link> 元素包含不同提要版本的 URL,而 <openSearch:> 元素则包含摘要统计信息。
最外层的 <feed> 元素包括一个或多个 <entry> 元素,分别对应一个日程表事件。每个 <entry> 包含更详细的信息,如标题、描述、发布日期、最近更新日期、事件提要 URL 以及每个事件的作者。分别用 <title>、<summary>、<published>、<updated>、<link> 和 <author> 元素表示。
返回列表