Hi Guys,

Do you want to embed trailers from YouTube dynamically to your website without any registration for API key? If No, then move on 😉 If Yes, read on. 😀

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.

So, basically idea is to get the video id and get the embed string like –


Let’s see the code.

Inputs needed are – movie name, year of release and the language. OR Just the Video ID, in case you know it. So, let’s create our input model.

namespace YouTubeTrailer\Bean;

class Movie {
	
	private $MovieName;
	
	private $YearOfRelease;
	
	private $VideoId;
	
	private $Language;
	
	public function setMovieName(string $movieName) {
		$this->MovieName = trim($movieName);
	}
	
	public function getMovieName() : string {
		return $this->MovieName;
	}
	
	public function setYearOfRelease(int $yearOfRelease) {
		$this->YearOfRelease = trim($yearOfRelease);
	}
	
	public function getYearOfRelease() : int {
		return $this->YearOfRelease;
	}
	
	public function setVideoId(string $videoId) {
		$this->VideoId = trim($videoId);
	}
	
	public function getVideoId() : string {
		return $this->VideoId;
	}
	
	public function setLanguage(string $language) {
		$this->Language = trim($language);
	}
	
	public function getLanguage() : string {
		return $this->Language;
	}
}

The final trailer data will be stored in Trailer.php

namespace YouTubeTrailer\Bean;

class Trailer {
	
	private $Id;
	
	private $Match;
	
	private $Width;
	
	private $Height;
	
	private $ParameterString;
	
	private $FrameBody;
	
	private $AllowFullScreen;
	
	private $ContentType;
	
	private $VideoId;
	
	public function setId(string $id) {
		$this->Id = $id;
	}
	
	public function getId() : string {
		return $this->Id;
	}
	
	public function setMatch(string $match) {
		$this->Match = $match;
	}
	
	public function getMatch() : string {
		return $this->Match;
	}
	
	public function setWidth(int $width) {
		$this->Width = $width;
	}
	
	public function getWidth() : int {
		return $this->Width;
	}
	
	public function setHeight(int $height) {
		$this->Height = $height;
	}
	
	public function getHeight() : int {
		return $this->Height;
	}
	
	public function setParameterString(array $arr) {
		$this->ParameterString = http_build_query($arr);
	}
	
	public function getParameterString() : string {
		return $this->ParameterString;
	}
	
	public function setFrameBody(int $frameBody) {
		$this->FrameBody = $frameBody;
	}
	
	public function getFrameBody() : int {
		return $this->FrameBody;
	}
	
	public function setAllowFullScreen(string $allowFullScreen) {
		$this->AllowFullScreen = $allowFullScreen;
	}
	
	public function getAllowFullScreen() : string {
		return $this->AllowFullScreen;
	}
	
	public function setContentType(string $contentType) {
		$this->ContentType = $contentType;
	}
	
	public function getContentType() : string {
		return $this->ContentType;
	}
	
	public function setVideoId(string $videoId) {
		$this->VideoId = trim($videoId);
	}
	
	public function getVideoId() : string {
		return $this->VideoId;
	}
}

The main class. Fetching of video id.

namespace YouTubeTrailer\Core;

use YouTubeTrailer\Bean\Movie;
use YouTubeTrailer\Bean\Trailer;
use YouTubeTrailer\Helper\YouTubeTrailerHelper;
use YouTubeTrailer\Exception\YouTubeException;

class YouTubeTrailer {
	
	public function getTrailerByNameAndYear(Movie $bean) : Trailer {
		
		$movieName = str_replace(' ', '+', "");
		$youtubePage = @file_get_contents('https://www.youtube.com/results?search_query='.$bean->getMovieName().'+'.$bean->getYearOfRelease.'+trailer&aq=1&hl='.$bean->getYearOfRelease);
		$match = "";
		if($youtubePage == "") {
			throw new YouTubeException("Trailer page for this video is not found.", 5);
		} else {
			$matches = array();
			if(@preg_match('~

 

~s', $youtubePage, $matches)) {
				$match = $matches[1];
			}
		}
		
		$helper = new YouTubeTrailerHelper();
		return $helper->populateTrailerAttributes($match, new Trailer());
	}
	
	public function getTrailerByVideoId(Movie $bean) : Trailer {
		$helper = new YouTubeTrailerHelper();
		return $helper->populateTrailerAttributes($bean->getVideoId(), new Trailer());
	}
}

Now, populate the Trailer object.

namespace YouTubeTrailer\Helper;

use YouTubeTrailer\Bean\Trailer;

class YouTubeTrailerHelper {
	
	public function populateTrailerAttributes(string $match, Trailer $bean) : Trailer {
		
		$bean->setId('ytplayer');
		
		$bean->setMatch($match);
		
		$bean->setWidth(557);
		
		$bean->setHeight(361);
		
		$parameters  = array (
			"cc_load_policy"	 	=> "1",
			"controls" 			 	=> "1",
			"disablekb" 		 	=> "1",
			"enablejsapi" 		 	=> "1",
			"fs" 					=> "0",
			"rel" 					=> "0",
			"showinfo" 				=> "0",
			"autohide" 				=> "1",
			"color" 				=> "white",
			"iv_load_policy" 		=> "1",
			"theme" 				=> "dark"
		);
		
		$bean->setParameterString($parameters);
		
		$bean->setFrameBody(0);
		
		$bean->setAllowFullScreen("true");
		
		$bean->setContentType("text/html");
		
		return $bean;
	}
}

Now you have Trailer object with data. To use it –

use YouTubeTrailer\Bean\Movie;
use YouTubeTrailer\Core\YouTubeTrailer;
use YouTubeTrailer\Bean\Trailer;

include_once 'autoload.php';

$movieBean = new Movie();
$movieBean->setMovieName($_GET['movie']);
$movieBean->setYearOfRelease($_GET['year']);
$movieBean->setLanguage($_GET['language']);
$trailerObj = new YouTubeTrailer();
$trailerBean = $trailerObj->getTrailerByNameAndYear($movieBean);
$id = $trailerBean->getId();
$type = $trailerBean->getContentType();
$width = $trailerBean->getWidth();
$height = $trailerBean->getHeight();
$video = $trailerBean->getMatch();
$param = $trailerBean->getParameterString();
$frame = $trailerBean->getFrameBody();
$fullscreen = $trailerBean->getAllowFullScreen();

$trailer = "
";

// echo $trailer;

That’s it.

You can browse the complete code from GitHub: https://github.com/niteshapte/youtube-trailer-2.0

and download directly – https://github.com/niteshapte/youtube-trailer-2.0/archive/master.zip

Feedback / comments are welcome.

Have a nice day ahead.

 

Loading