Hi guys,

In March 2010, I published a package YouTube Trailer in PHPClasses.org for fetching YouTube trailer, dynamically.
yt2

You must have seen trailers of a movie/s embedded into a webpage. YouTube allows you to embed videos in external site pages, but you need to know the identifier of the specific video you want to embed. So, it’s basically a Ctrl + C and Ctrl + P activity, what we call as hard coding.

YouTube Trailer package can be used to search and embed movie trailers from YouTube. It can send an HTTP request to retrieve and parse the YouTube search result pages for a trailer of a given movie name. The class extracts the identifier of the first result video and generates HTML to embed that video in a Web page. Hence this class can automatically embed trailers of movies from YouTube just giving the movie names.

 

YouTube allows you to embed videos in external site pages, but you need to know the identifier of the specific video you want to embed. This class can automatically embed trailers of movies from YouTube just giving the movie names.

 

Now, let’s come to the code. Below is the class that contains the main functionality.

class.MovieTrailer.php

 <?php
/**
* Fetch movie trailer from YouTube
*
* @author Nitesh Apte
* @version 1.0
* @access public
* @License GPL
*/

class MovieTrailer
{
    /**
    * Private variables for website interaction
    */
    private $movieName;
    private $movieYear;
    private $page;
    private $embed;
    private $matches;

    /**
    * Fetch movie trailer from YouTube
    *
    * @param $movie Movie Name
    * @param $year Movie Year
    * @return none
    */
    public function __construct($movie, $year)
    {
        $this->movieName = str_replace(' ', '+', $movie);
        $this->movieYear = $year;
        $this->page = file_get_contents('http://www.youtube.com/results?search_query='.$this->movieName.'+'.$this->movieYear.'+trailer&aq=1&hl=en');

        if($this->page)
        {
            if(preg_match('~<a .*?href="/watch\?v=(.*?)".*?</div>~s', $this->page, $this->matches))
            {
                $this->embed = '<embed src="http://www.youtube.com/v/'.$this->matches[1].'&autoplay=1&fs=1" type="application/x-shockwave-flash" wmode="transparent" allowfullscreen="true" width="557" height="361"></embed>';
                echo $this->embed;
            }
        }
        else
        {
            echo "<b>check internet connection.....</b>";
        }
    }
}
?>

The logic is to search the page on YouTube based on two criteria:
a)        the movie name and
b)        the year of release of movie. This is optional but better to pass it as two movies’ name could be same but 99.9% chances are that year of release is different.

When search result is fetched, parse the content and extract the first identifier and embed it in embed tag.

Usage: index.php

<?php
include "class.MovieTrailer.php";
new MovieTrailer(“Zombieland”, “2009”);
?>

 

That’s it.

You can check out the DEMO here: The MovieDB 3.0. When you are there, click on the movie name and you can see the trailer in a pop up window. That’s something extra I did to make it look more fancy 😉

Download: YouTube trailer with FancyBox

Download from PHPClasses.org: http://www.phpclasses.org/browse/package/6049/download/targz.html. You need to be a registered user in PHPClasses.org.

That’s all folks.

Hope this package will be useful to you guys especially to those who are working for Entertainment industry.

Soon I will be publishing to fetch movie details from IMDB.

Questions and feedback are very much welcomed.

Loading