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
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    Chris Coyier&#8217;s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    Disable Autocomplete, Autocapitalize, and Autocorrect

    Mobile and desktop browser vendors do their best to help us not look like idiots by providing us autocomplete, autocorrect, and autocapitalize features.  Unfortunately these features can sometimes get in the way;  we don't always want or need the help they provide.  Luckily most browsers allow...

  • By
    MooTools, Mario, and Portal

    I'm a big fan of video games. I don't get much time to play them but I'll put down the MacBook Pro long enough to get a few games in. One of my favorites is Portal. For those who don't know, what's...

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!