Skip to content

PHP Display YouTube Video Class, OOP. Embed YouTube Video.

Share Button
 <?php

/**
*   YouTube video Class - display video 
*   Features(Briefly): 
*       - Check if video Exists
*       - Set Video width, length, frameborder, allowfullscreen
*       - Display YouTube Video
*/

class DisplayYouTubeVideo{
    
    /** Set Error Message */
    private function setErrorMsg(){
        $notExists = "Error!! Video Broken or Doesn't Exists";
        return $notExists;
    }
    
    /** Get Error Message */
    private function getErrorMsg(){
        return $this->setErrorMsg();
    }
    
    /** Check if Video Exists or URL provided is Valid */
    private function checkVideo($YouTubeVideo){
        /** Check file_get_contents on http://php.net/manual/en/function.file-get-contents.php     
        *   suppress the warning(FALSE) by putting an @ in front of file_get_contents.
        */
        $json_output = @file_get_contents("http://www.youtube.com/oembed?url=".$YouTubeVideo."&format=json"); 
        /** Check $json_output result */
        if($json_output != FALSE || $json_output != 0){
            /** json_decode(), takes JSON encoded string and converts it into a PHP variable.*/
            $json = json_decode($json_output, true);
            return $json;
            
        } else {
            /** Echo Error */
            echo $this->getErrorMsg();
            return false;
        }
    }
    
    /** Width of the video */
    private function getWidth(){
        return 450;
    }
    
    /** Height of the video */
    private function getHeight(){
        return 365;
    }

    /** Frameborder */
    private function getFrameborder(){
        return 1;
    }
    
    /** Display YouTube video in <iframe> */
    private function displayVideo($youtube_id){
        echo "<iframe ";
        echo "width='".$this->getWidth()."' ";
        echo "height='".$this->getHeight()."' ";
        echo "src='https://www.youtube.com/embed/{$youtube_id}?'";
        echo " frameborder='".$this->getFrameborder()."' ";
        echo "allowfullscreen";
        echo ">";
        echo "</iframe>";
    }
    
    /** Get Youtube video ID 
    *   Will nedd ID to dipslay Video in <iframe>
    */
    private function getYouTubeIdFromURL($videoUrl)
    {
    /** parse_url() - Parse URL & returns its components 
    *   Returns an associative array of components that the URL represents 
    *   If a second arg/$component is specified i.e. parse_url($url, $component), 
    *   a string/integer is returned instead.
    */
        
      $urlStr = parse_url($videoUrl, PHP_URL_QUERY);
    /** parse_str() - Parse string into variables 
    *   If the second parameter arr is present, variables are stored 
    *   as array elements instead. 
    */
      parse_str($urlStr,$videoID);

        /** If array_key 'v' exists print/return it's value/Video ID */    
        return isset($videoID['v']) ? $videoID['v'] : false;
    }
   
    /** Get YouTube Link Result...*/
    public function getResult($YouTubeVideo){
        /** Check if YouTube video Exists */
        if($this->checkVideo($YouTubeVideo)){
            /** Display Video...*/
            return $this->displayVideo($this->getYouTubeIdFromURL($YouTubeVideo));    
        }
    }
    
}

/** Instance of a class */
$video = new DisplayYouTubeVideo();
/** Get Result by passing YouTube Video Link */
$video->getResult("https://www.youtube.com/watch?v=pvAsqPbz9Ro");

?>

Result:

linuxGet.co.uk_YouTubeVideo

Share Button