Log in

View Full Version : SimpleXML and Google Calendar



Relics
02-05-2009, 07:11 AM
Hello all,

I'm very new to SimpleXML and I'm learning how to do parsing XML data. I've got a code bit that I'm trying to use to pull Google Calendar data and display it on a site. I don't need anything more than something to pull the data and display it. Below is the code that I'm using:



<?php
$userid = 'username%40googlemail.com';
$magicCookie = 'cookie';

// build feed URL
$feedURL = "http://www.google.com/calendar/feeds/USERNAMEREMOVED/public/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);
$link = stripslashes($entry->id);

echo "<li>\n";
echo "<h2>$title</h2>\n";
echo "$summary <br/>\n";
echo "$link <br/>\n";
echo "</li>\n";
}
?>

What I need is to display the link back to the Google Calendar event when clicked. The problem is I don't know how to tell the SimpleXML to use the link in XML data tagged: <link rel="alternate" .....>. There is also a <link rel="self"....> in the code, so adding "$link = stripslashes($entry->link); doesn't work. If anyone can tell me the code I need to use, I would appreciate it. Thanks

Lymedo
10-02-2009, 05:45 PM
nest this within the foreach statement that you have already....

foreach ($entry->link as $link){
if($link['rel']=='alternate'){
echo "<h2><a href='" .$link['href'] . "'>" . $entry->title . "</a></h2>";
}
}