How to build a movie trailer API using gData

An important feature of each movie site is trailers, so it is for sharemovi.es! And where should we look for trailers? YouTube! But how can we program to get the corresponding trailer video when inputting the movie title? Use gData!

First of all there is a complete YouTube API available, but my solution doesn’t require much if any of it. It really comes down to getting the right feed details and parse the XML:

1. First make a query to the gData API (a REST-inspired technology for reading, writing, and modifying information on the web – see also this video: it has been around for quite some time!)

$feedURL = 'http://gdata.youtube.com/feeds/api/videos?q='
  .$q.'&start-index=1&max-results=1';

$q is the movie string to query. the+movie+name+’trailer’ works best for me. And we want just the first hit to appear in the feed.

2. Read the feed into a SimpleXML object

$sxml = simplexml_load_file($feedURL);
// and iterate over entries in feed
foreach ($sxml->entry as $entry) {
   // get nodes in media: namespace for media information
   $media = $entry->children('http://search.yahoo.com/mrss/');
   // get video player URL
   $attrs = $media->group->player->attributes();
   $trailer = $attrs['url'];
}

(I found a much more detailed example on the IBM site, but this bit will do for what I want to show ..)

3. Some final tweaking: to embed the gathered URL in a html <object> we need to swap ‘watch?v=’ with ‘v/’. Luckily I played with YouTube URLs before because this is not very obvious.

$trailer = str_replace('watch?v=','v/',$trailer);

.. and we embed the result in the standard embed code YouTube provides to us

<object width="640" height="385"><param name="movie"
value="<?php echo $trailer ;?>?fs=1&amp;hl=en_US"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="<?php echo $trailer ;?>?fs=1&amp;hl=en_US" type="application/x-shockwave-flash"
allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed>
</object>

You can see the result on sharemovi.es.

I use the jQuery Boxy plugin to present the output as a nice Facebook-like overlay:

A. Click on “View trailer”

B. Overlay with the result of the trailer script that found “The Ghost Writer”‘s trailer on YouTube:

Update (09.08.2011)

Some readers requested the source. I put it on Github. Let me know where you roll it out :)

  • http://sector-seven.net DannyB

    Great post, plain and simple and works like a charm. I just wonder if “movie+name+trailer” as a query would always return the correct result.

    Thanks for sharing this.

    • Anonymous

      Thanks Danny for your compliment. It works amazingly well, but it is not 100% correct. The success rate is ok though for sharemovi.es. Cheers

  • Dean

     I would be very interested in a copy of your code…

    Could you possibly email me…????dean@datalynk.co.uk

    • Anonymous

      will mail you

    • http://twitter.com/bbelderbos Bob Belderbos

      Dean, I just mailed it to you. So see PM !

  • http://www.facebook.com/eduard.mikhailovsky Eduard Mikhailovsky

    Very interested !
    Could you please share your code ?
    I want try use on wordpress based site.

    Many thanks.

    • Anonymous

      Thanks! Yep will look for it and send it to you. And/or get a link to it in this post

    • Anonymous

      btw friendsjukebox.com uses the same script to yield music videos for an artist

  • http://www.facebook.com/eduard.mikhailovsky Eduard Mikhailovsky

    Very interested !
    Could you please share your code ?
    I want try use on wordpress based site.

    Many thanks.

  • http://www.facebook.com/eduard.mikhailovsky Eduard Mikhailovsky

    Very interested !
    Could you please share your code ?
    I want try use on wordpress based site.

    Many thanks.

  • bonkers

    Well played, sir. I do like what you did there. Compliments from Germany

    • http://bobbelderbos.com/ Bob Belderbos

      thank you. I like to experiment with APIs :) … and this one was a perfect match for sharemovi.es

  • http://www.facebook.com/people/Sid-Labs/100000804868370 Sid Labs

    What source are you using to get the Movies DB?

    • http://bobbelderbos.com/ Bob Belderbos

      what do you mean? I included the script, it’s in PHP : http://bobbelderbos.com/src/trailer – or you mean the source where I get the data? that would be this part: $feedURL = ‘http://gdata.youtube.com/feeds/api/videos?q=’.$q.’&start-index=1&max-results=1′;

  • http://bobbelderbos.com/ Bob Belderbos

    interested? send me an email

  • anurag

    Hi Bob, can you please tell me how i can get api for URL like this “http://www.youtube.com/results?search_query=hindi+movies+trailers+2013&filters=month%2C+short&lclk=short” can you mail me the API at “anurag.s@parikshalabs.com” please please i am newbie for this… so i want you to help me please. i am really thank full to you

    • http://bobbelderbos.com/ Bob Belderbos

      what are you trying to achieve? you want that URL feed to display on a page of yours?

  • Marc Donald Ahoure

    Hello sir, my name’s
    Marc Donald I was searching since 2 weeks to get a correct YouTube api
    to retrieve a movie trailer based on users search I am newbie in web development
    I need please your full code to add to my site please mail me at dmcorporation2014@gmail.com. I
    hope you understood me, sorry for my bad English. Thanks you a lot for your
    great tutorial

  • Marc Donald Ahoure

    Sorry for worry you again sir, I Solved it Thanks again !!!

    • http://bobbelderbos.com/ Bob Belderbos

      ok good to hear, what are you building?

© 5303 Bob Belderbos. All rights reserved.

- If you like something here, link to it instead of copy+paste.
- Disclaimer: ideas expressed on my blog are mine, and have nothing to do with the current/previous employers.
- Proudly using Wordpress and the Insider Theme on Bluehost