Fun with PHP and Simple XML - Amber Alerts
Posted on June 17th, 2008 in American Idol, Geek Stuff, On Marriage, PHP, Web Development
A few weeks ago I was toying with XML. I had a few minutes to spend teaching myself so I decided that I would use that to work on something that I desperately need to work on. XML.
And what better way to learn a little XML stuff that on the Amber Alert system? So I began my journey.
Brief aside: I hate the way Wordpress does not know how to handle PHP code and DTDs in their markup parser. This should have been taken care of long ago. Seriously guys, is it that hard?
So here is the code I came up with for rendering the Amber Alert feed. No, this is not some whizbang type of code. It is simple. It is easy. And it can be easily applied to almost any XML output you want.
Here is a working example of this script.
< ?php
/**
* Grab the contents of the output of the Amber Alert feed
*/
$xmlstr = file_get_contents('http://amberalert.com/rss-feed/consortium');
/**
* Pass the string XML output to the Simple XML class creating
* an XML object.
*/
$xml = new SimpleXMLElement($xmlstr);
?>
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<div id="page-wrap">
<div id="page-header">
< ?php /* Snag the title of the feed and the publication date */ ?>
<h1>< ?php echo $xml->channel->title ?></h1>
<h2>Publication Date: < ?php echo $xml->channel->pubDate ?></h2>
</div>
<div id="page-body">
<a title="<?php echo $xml->channel->title ?>" href="< ?php echo $xml->channel->link ?>">Source of this feed</a>
<ul>
< ?php /* Ok, lets loop the actual feed items and output them with a little style */ ?>
< ?php $off = false; $first = true; foreach ($xml->channel->item as $item): ?>
<li class="item<?php if ($off): ?> offset< ?php endif; ?>< ?php if ($first): ?> first< ?php $first = false; endif; ?>">
<h3 class="item-header">< ?php echo $item->title, ' - Published ', $item->pubDate ?></h3>
<div class="item-description">< ?php echo $item->description ?></div>
<div class="item-link"><a title="Read more..." href="<?php echo $item->link ?>">Read more...</a></div></li>
< ?php $off = !$off; endforeach; ?></ul>
</div>
</div>And if you absolutely have to have to CSS to go with it…
* { margin: 0; padding: 0; } html { font: 10pt/15pt Verdana, Trebuchet, Verdana, Arial, sans-serif; color: #666; background: #efefef; } #page-wrap { margin: 30px; padding: 15px; border: double 3px #800; } #page-header { text-align: center; padding-bottom: 10px; border-bottom: 1px solid #333; } #page-header h1 { padding-bottom: 10px; } #page-header h2 { font-size: 13pt; } #page-body ul { list-style: none; } #page-body ul li { border-bottom: 1px solid #ccc; border-right: 1px solid #ccc; border-left: 1px solid #ccc; padding: 10px; } #page-body ul li.first { border-top: 1px solid #ccc; } #page-body ul li.offset { background: #dedede; } #page-body .item-header { font-size: 10pt; font-weight: bold; } #page-body .item-description, #page-body .item-link { padding-left: 20px; } #page-body .item-link { font-size: 8pt; line-height: 16pt; padding-bottom: 10px; }
I hope you can use some of the stuff that is in here. It isn’t much, but perhaps it is just enough to help you play a little with XML.
No Responses to “Fun with PHP and Simple XML - Amber Alerts”
There are currently no comments on this post. But you can change that...