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
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

Incredible Demos

  • By
    AJAX For Evil:  Spyjax with jQuery

    Last year I wrote a popular post titled AJAX For Evil: Spyjax when I described a technique called "Spyjax": Spyjax, as I know it, is taking information from the user's computer for your own use — specifically their browsing habits. By using CSS and JavaScript, I...

  • By
    Facebook Sliders With Mootools and CSS

    One of the great parts of being a developer that uses Facebook is that I can get some great ideas for progressive website enhancement. Facebook incorporates many advanced JavaScript and AJAX features: photo loads by left and right arrow, dropdown menus, modal windows, and...

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!