我尝试使用PHP从Google Cal中提取一些数据时遇到错误。这是代码:
$url = $_POST['url'];
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
$ns= $feed->getNameSpaces(true);
foreach ($feed->entry as $entry) {
$eventdetail = $entry->children($ns["gd"]);
$when_atr = $eventdetail->when[0]->attributes();
$title = $entry->title;
echo "<div class='eventTitle'>".$title . "</div>";
$where = $eventdetail->where[0]->attributes();
echo "<div class='eventLocation'>" . $where . "</div>" . '<br />';
$start = new DateTime($when_atr['startTime']);
echo "<div class='eventTime'>".$start->format('D F jS, g:ia') . " to ";
$end = new DateTime($when_atr['endTime']);
echo $end->format('g:ia')."</div>" . '<br />' ;
}
标题工作得很好,但对于&#34;当&#34;和&#34;其中&#34;我遇到了代码when[0]->attributes();
和where[0]->attributes();
的问题。
我得到的错误是
致命错误:在非对象上调用成员函数attributes()
我该怎么做才能防止这种情况发生?