Skip to content

PHP Class, Check if YouTube video Link Exists and display video info

Share Button
 <?php

class CheckYouTubeVideo {

    /** Set Success Message */
    private function setSuccessMsg(){
        $this->exists = "YouTube Video Exists :)";
        return $this->exists;
    }
    
    /** Set Error Message */
    private function setErrorMsg(){
        $this->notExists = "Error... Video Broken or Doesn't Exists...";
        return $this->notExists;
    }
    
    /** Get Success Message */
    public function getSuccessMsg(){
        return $this->setSuccessMsg();
    }
    
    /** Get Error Message */
    public function getErrorMsg(){
        return $this->setErrorMsg();
    }
    
    /** Check if Video Exists or URL provided is Valid */
    public 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){
            /** Takes JSON encoded string and convert it into a PHP variable.*/
            $this->json = json_decode($json_output, true);
            /** Return encoded string */
            return $this->json;
            
        } else {
            /** Echo Error */
            echo $this->getErrorMsg();
            return false;
        }
    }
    
    /** Get YouTube Link Result...*/
    public function getResult($YouTubeVideo){
            return $this->checkVideo($YouTubeVideo);
    }
} // Class Ends

                                
/** Create Instance of a class */
$checkVideo = new CheckYouTubeVideo();
/** Assign method with result to variable */
$checkLink = $checkVideo->getResult("https://www.youtube.com/watch?v=pvAsqPbz9Ro");   

/** if Video Exists do Something... */
if($checkLink){
    /** Echo Success Message*/
    echo $checkVideo->getSuccessMsg();

    
    /** SOME EXTRA FOR YOU..... */
    
    echo "<br />";

    /** Display Video Title */
    echo "<strong>Video Title:</strong> " . $checkVideo->json['title'];

    echo "<br />";

    /** Display Video Author */
    echo "<strong>Video Author:</strong> " . $checkVideo->json['author_name'];  
   
} // End if Statment

?>

RESULT:

YouTube Video Exists!

Share Button