PHP Classes

How to fetch trailers from YouTube using PHP - YouTube Trailer package blog

Recommend this page to a friend!
  All package blogs All package blogs   YouTube Trailer YouTube Trailer   Blog YouTube Trailer package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to fetch trailers...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 47

Last month viewers: 18

Package: YouTube Trailer

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.




Loaded Article

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 ;-)

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.

[From my blog - http://blogs.niteshapte.com/2013-06-21-how-to-fetch-trailers-from-youtube-using-php.htm]




You need to be a registered user or login to post a comment

1,611,040 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   YouTube Trailer YouTube Trailer   Blog YouTube Trailer package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to fetch trailers...