PHPTVDB: Using PHP to Retrieve TV Show Information

By  on  
The Office (UK)

I'm a bigtime TV/movie buff. If it's ever seen the light of day on HBO or BBC America, I've seen it (and quite possibly filmed my own made-for-TV-short-film). Unfortunately IMDB doesn't provide an official API for movies and TV shows but thankfully TVDB does.

Retrieving information from TVDB is a breeze using a small library called PHPTVDB written by Ryan Doherty. With a little PHP knowledge and TVDB API key, you can easily grab data about any TV show.

The PHP

/* include the library */
require 'phptvdb-1.0.2/TVDB.php';

/* get the office! */
$shows = TV_Shows::search('The Office UK');

/* get the first show */
$show = $shows[0];

/* a few shortcuts / formatted vars */
$firstAired = date('l, F j, Y',$show->firstAired);
$imdb_url = "http://imdb.com/title/$show->imdbId";

/* spit out the show information */
echo "<h2>$show->seriesName</h2>
		<div class=\"meta\">
		<strong>Rating:</strong> $show->rating •
		<strong>Debuted:</strong> $firstAired on $show->network •
		<a href=\"$imdb_url\">$show->seriesName on IMDB</a>
		</div>
		<p>$show->overview</p>";
		
/* my favorite episode info  */
$episode = $show->getEpisode(1,4);
echo "<div class=\"righty\">
			<h3>Favorite Episode:  $episode->name</h3>
			<p>$episode->overview</p>
		</div>";

/* spit out the actors */
echo '<h3>The Cast</h3><ul>';
foreach($show->actors as $actor) {
	echo "<li>$actor</li>";
}
echo '</ul><br />';

/* learn more link */
echo "<p><a href=\"http:/* imdb.com/title/\">Learn more about \"$imdb_url\" on IMDB</a></p>";

The above is just a sample usage of this great PHP library. You can find documentation here. Remember to try to be specific in your initial show search. I recommend going to IMDB or TVDB to get the show's "official" name to confirm you are being as specific as possible.

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Google Font API

    Google recently debuted a new web service called the Font API.  Google's Font API provides developers a means by which they may quickly and painlessly add custom fonts to their website.  Let's take a quick look at the ways by which the Google Font...

  • By
    Use Custom Missing Image Graphics Using MooTools

    Missing images on your website can make you or your business look completely amateur. Unfortunately sometimes an image gets deleted or corrupted without your knowledge. You'd agree with me that IE's default "red x" icon looks awful, so why not use your own missing image graphic? The MooTools JavaScript Note that...

Discussion

  1. francesco

    Awesome!
    you know if exist a script can retrive information from freedb.org ?
    Tnx

  2. This is so cool Dave.

  3. Hey, thanks for writing up a blog post about PHP::TVDB! I seem to have lost some pages about it and was using Google to see if they were still cached. That’s how I found this article.

    Anyway, thanks, it’s nice to see that my code is useful!

  4. Daniel Wall

    This only gives me an empty page. Nothing is printed and the source is null.

  5. Weasel

    It’s worth noting that this requires the php5-curl package which may not be installed by default.

    A tidy post, just what I was after – thanks. :-)

  6. Kurt

    Hey Dave, I know it’s an old post, but when I first checked it out a few months ago, everything seemed to work. I was able to use parts of the code and set it up in my own environment. Unfortunately, soon after, mine stopped working and upon returning to this page, it looks like your demo also stopped working – any idea why?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!