Quantcast
Channel: AF-Design » PHP
Viewing all articles
Browse latest Browse all 10

Zend Framework’s Podcast Reader Documentation

$
0
0

Using the Zend Framework can be incredibly frustrating due to the lack of clear documentation. Today I ran into an issue while trying to display an additional field from a RSS feed. The RSS feed is fairly simple, it was originally built to feed iTunes and we’re simply repurposing it. Originally, the client only needed the title and mp3 file. However, a new podcast with multiple contributors made it necessary to include the author field in the display that we’re now generating as well.

The following is a snippet of the feed that’s being generated by the other service.

...
<item> 
<title>i GaGa - Podcast #1</title> 
<author>Steve Jobs</author> 
<itunes:author>Steve Jobs</itunes:author> 
<enclosure url="http://www.example.com/d/pc001.mp3" length="2500000" type="audio\/mpeg" /> 		<guid>http://www.example.com/d/pc001.mp3</guid> 
<pubDate>Fri, 28 May 2010 00:00:00 GMT</pubDate> 
<itunes:keywords>apple, iphone, twitter</itunes:keywords> 
</item> 
...

As you can see it’s incomplete and there are some unnecessary fields included, but overall, the data is there and should be easy to scrape out. The basic code to grab the title and attachment was already scripted using the Zend Feed Reader.

function parse($xml_string){
	$feed = Zend_Feed_Reader::importString($string_data);
	$data = array(
		'title' => $feed->getTitle(),
		'description' => $feed->getDescription(),
		'link' => $feed->getLink(),
		'entries' => array(),
	);
	foreach($feed as $entry){
		$item = array(
				'id' => $entry->getId(),
				'title'=>$entry->getTitle(),
				'enclosure' => $entry->getEnclosure(),
		);
		$data['entries'][] = $item;
	}
	return $data;
}

So just adding the getAuthor() method, should grab the author field, at least that’s what the official documentation says. An alternate method getAuthors() should return an associative array. However, this is clearly overkill in this scenario because we already know that data doesn’t exist. Ultimately, neither of these actually worked with my content. Both methods simply returned NULL.

It turns out it’s VERY simple to get the author value from the feed above. The ability to extend the Zend Framework easily is a real strength, but it’s not intuitive. After skimming a few blog articles about extending the reader classes, I decided to go back and re-read the documentation on extending the reader classes. I thought if I could find a working extension, I could use that as a starting point for writing my own. It turns out that there’s already a Podcast extension which is excatly what I had set out to write. Oh, and it’s also enabled by default!

Great! it’s there, I don’t even have to register it because it’s registered by default… W00t! It just isn’t working, or more accurately, I have no idea how to use it. Each of the built in extensions have their own methods for grabbing information. Finding documentation on these methods is difficult. This is one place the php.net documentation really excels and I had expected Zend would provide something similar in nature. Unfortunately I was wrong. I did figure out what methods existed (and what they did) after grep’ng the code. The change I needed was simple, use getCastAuthor() to set my $item[‘author’] field, my code was ready.

function parse($xml_string){
	...
	foreach($feed as $entry){
		$item = array(
				'id' => $entry->getId(),
				'title'=>$entry->getTitle(),
				'author'=>$entry->getCastAuthor(),
				'enclosure' => $entry->getEnclosure(),
		);
		$data['entries'][] = $item;
	}
	return $data;
}

I suspect using the Zend Studio product includes much of this documentation making it easier to write code leveraging the power of the Framework.

The Missing Manual

To save some of you the hassle of having to grep the code and read it yourself, I offer this guide to the methods added with the Podcast extension. This is current as of revision 22418 from the subversion standard trunk. If you are attempting to create an iTunes compliant feed, be aware that there is a separate iTunes writer extension built directly into the Framework as well. Also be sure to read the Apple RSS Specification documents that outlines exactly how they use each field.

Feed (Zend_Feed_Reader_Extension_Podcast_Feed)

These additional methods are made available to the feed, which is what’s returned from the call to Zend_Feed_Reader::importString() or whatever import method you are using.

getCastAuthor()
This will return the contents of <itunes:author> tag.

getBlock()
This will return the contents of <itunes:block> tag.

getCategories()
This will return the contents of the <itunes:category> as an array of values.

getExplicit()
This will return value the of the <itunes:explicit> tag.

getImage()
This will return the href value of the <itunes:image>, suitable for use in an <img> tag.

getKeywords()
This will return the contents of the <itunes:keywords> tag.

getNewFeedUrl()
This will return the contents of <itunes:new-feed-url> tag.

getOwner()
This will return a string that concatenates intelligently the values contained in <itunes:owner>’s <itunes:email> and <itunes:name> values.
For example, the following XML would return: tony@bp.com (Tony Hayward)

<itunes:owner>
   <itunes:name>Tony Hayward</itunes:name>
   <itunes:email>tony@bp.com</itunes:email>
</itunes:owner>

getSubtitle()
This will return the contents of <itunes:subtitle> tag.

getSummary()
This will return the contents of <itunes:summary> tag.

Entry (Zend_Feed_Reader_Extension_Podcast_Entry)

These additional methods are made available on a per entry item level.

getCastAuthor()
This will return the contents of <itunes:author> tag.

getBlock()
This will return the contents of <itunes:block> tag.

getDuration()
Unique to the item field, this will return the contents of <itunes:duration> tag.

getExplicit()
This will return value the of the <itunes:explicit> tag.

getKeywords()
This will return the contents of the <itunes:keywords> tag.

getSubtitle()
This will return the contents of <itunes:subtitle> tag.

getSummary()
This will return the contents of <itunes:summary> tag.


Viewing all articles
Browse latest Browse all 10

Trending Articles