Display link from XML using PHP simpleXML
Here is the code I have written, It should fetch the first entry in the atom开发者_运维问答 feed from GitHub and display the commit description and a link to the commit on GitHub:
$url = "https://github.com/moodle/moodle/commits/master.atom";
$rss = simplexml_load_file($url);
if($rss) {
$items = $rss->entry;
$i = 0;
foreach($items as $item) {
$title = $item->title;
$link = $item->link;
echo '<li class="github">';
echo '<span>GitHub - moodle:master</span>';
foreach($link as $links) {
echo $links->href;
echo "<a href=".$links->href.">";
}
echo $title;
echo '</a>';
echo "</li>";
if(++$i == 1) break;
}
}
However I do not seem to be able to display the href from the link entity.
Can anyone explain or adjust the code to get the link displaying?
Elements and attributes are handled differently in SimpleXML. You should be able to pull it out like this instead:
foreach($link as $links) {
echo $links['href'];
echo "<a href=".$links['href'].">";
}
Also, you can check http://www.php.net/manual/en/simplexml.examples-basic.php to see some simple usage examples.
精彩评论