我从公共谷歌日历中获得了XML提要.看起来像这样:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='................' xmlns:gd='http://schemas.google.com/g/2005'>
<id>http://www.google.com/calendar/feeds/........./public/full</id>
<updated>2009-08-24T10:57:00.000Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/>
<title type='text'>Sports Events</title>
.....
<entry>
<id>...........</id>
<published>2009-08-14T00:29:58.000Z</published>
<updated>2009-08-14T00:29:58.000Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/>
..............
<georss:where>
<gml:Point>
<gml:pos>11.111111 -22.22222</gml:pos>
</gml:Point>
</georss:where>
<gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'/>
<gd:transparency value='http://schemas.google.com/g/2005#event.transparent'/>
<gCal:uid value='.........@google.com'/>
<gCal:sequence value='0'/>
<gd:when startTime='2009-10-03' endTime='2009-10-04'/>
.............
Run Code Online (Sandbox Code Playgroud)
现在到PHP:
$calendar = simplexml_load_file('http://my.feed.com');
foreach ($calendar->entry as $item) {
//here I get the whole <entry> node
$gd = $item->children('http://schemas.google.com/g/2005');
// now in $gd I have all nodes like <gd:XXXXX>
}
Run Code Online (Sandbox Code Playgroud)
问题是如何从这里获得价值?
<gml:pos>11.111111 -22.22222</gml:pos>
Run Code Online (Sandbox Code Playgroud)
它不存在于我的$ gd变量中,如何获取此节点?
Ken*_*itz 10
我强烈建议使用这个库:https://github.com/collegeman/coreylib.它会让所有事情从头到尾都变得令人难以置信.
当然有几种方法可以解析XML.使用XPath在PHP网站上显示Google日历事件(请参阅"使用PHP解析Google日历源"),并将PHP应用程序与Google日历集成,这是两个包含示例代码等的综合资源.
就个人而言,我使用了以下方法:
$doc = new DOMDocument();
$doc->load('http://my.feed.com');
$entries = $doc->getElementsByTagName("entry");
foreach ( $entries as $entry ) {
$titles = $entry->getElementsByTagName("title");
$title = $titles->item(0)->nodeValue;
$times = $entry->getElementsByTagName("when");
$startTime = $times->item(0)->getAttributeNode("startTime")->value;
$when = date("l jS \o\f F Y - h:i A", strtotime($startTime));
// ...
}
Run Code Online (Sandbox Code Playgroud)
为了访问georss命名空间等看看(和它的输出)
foreach ($doc->getElementsByTagNameNS('*', '*') as $element) {
echo 'localName: ', $element->localName, ', prefix: ', $element->prefix, "\n";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18724 次 |
| 最近记录: |