在 PHP 应用程序中集成 Google Calendar(2)
 
- UID
- 1066743
|

在 PHP 应用程序中集成 Google Calendar(2)
使用 SimpleXML 检索事件列表下面看一个使用 PHP 处理 Google Calendar 提要的例子。清单 2 读取 中的提要并使用 SimpleXML 提取有关的数据,然后再将它转化为页面:
清单 2. 使用 SimpleXML 检索事件列表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
| <!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Listing calendar contents</title>
<style>
body {
font-family: Verdana;
}
li {
border-bottom: solid black 1px;
margin: 10px;
padding: 2px;
width: auto;
padding-bottom: 20px;
}
h2 {
color: red;
text-decoration: none;
}
span.attr {
font-weight: bolder;
}
</style>
</head>
<body>
<?php
$userid = 'username%40googlemail.com';
$magicCookie = 'cookie';
// build feed URL
$feedURL = "http://www.google.com/calendar/feeds/$userid/private-$magicCookie/basic";
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// get number of events
$counts = $sxml->children('http://a9.com/-/spec/opensearchrss/1.0/');
$total = $counts->totalResults;
?>
<h1><?php echo $sxml->title; ?></h1>
<?php echo $total; ?> event(s) found.
<p/>
<ol>
<?php
// iterate over entries in category
// print each entry's details
foreach ($sxml->entry as $entry) {
$title = stripslashes($entry->title);
$summary = stripslashes($entry->summary);
echo "<li>\n";
echo "<h2>$title</h2>\n";
echo "$summary <br/>\n";
echo "</li>\n";
}
?>
</ol>
</body>
</html>
|
显示了输出的结果:
图 1. 显示 SimpleXML 检索到的事件列表的 Web 页面 的代码中,simplexml_load_file() 对象向提要 URL 发送请求并把响应变成 SimpleXML 对象。然后遍历响应中的 <entry> 元素,通过 foreach() 循环检索 中的信息。<entry> 下的子节点用 SimpleXML 对象属性表示——比如 <title> 节点表示为 $entry->title,<summary> 节点表示为 $entry->summary,依此类推。
这里的提要 URL 引用了用户的私有日程表提要,包括用户的电子邮件地址和 magic cookie,后者用于只读访问日程表数据而不需要获得授权。如前所述,获得该提要 URL 需要手工完成:访问相应的 Google Calendar 页面并将 URL 从日程表设置中手工复制到 PHP 脚本中。 |
|
|
|
|
|