Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • To check if the video exist in YouTube

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.24k
    Comment on it

    Hello readers, today I will guide you about "How to Check Video Exist in Youtube through URL and ID using PhP?".

    As we all know, in many website's generally videos comes from Youtube. Sometimes the videos are not present on the Youtube but still their links are present in our database but we didn't know whether is it working or not at run time. Also it occupies space and show video not found error, which leaves very bad effect on our website users. So, to avoide that I have created this solution to check video exists or not on Youtube. 

    You can copy the code and integrate in your PHP code.

     <?php
    $idVideo = $_GET['video']; // Video id goes here
    $link = explode("?v=",$idVideo);
    
    if(isset($_GET['btnsub'])) {
    
    	if ($link[0] == 'https://www.youtube.com/watch') { // call function video_exists()
    	    
    	    $youtubeUrl = "http://www.youtube.com/oembed?url=$idVideo&format=json";
        	$docHead = get_headers($youtubeUrl);
    
        	if (substr($docHead[0], 9, 3) !== "404") {
    	        echo "<iframe width='560' height='315' src='https://www.youtube.com/watch?v=".$link[1]."' frameborder='0' allowfullscreen></iframe>";
    	    } else {
    	        echo "<div class='error'>There is no youtube video on (".$idVideo.").</div>";
    	    }
    	} elseif ($idVideo) { // check video through video Id
    	    
    	    $youtubeUrl = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$idVideo&format=json";
        	$docHead = get_headers($youtubeUrl);
        	if (substr($docHead[0], 9, 3) !== "404") {
    	        echo "<iframe width='560' height='315' src='https://www.youtube.com/embed/".$idVideo."' frameborder='0' allowfullscreen></iframe>";
    	    } else {
    	        echo "<div class='error'>There is no youtube video on (".$idVideo.").</div>";
    	    }
    	}
    	else {
    	    echo "<div class='error'>There is no youtube video on (".$idVideo.").</div>";
    	}
    }
    ?>
    

     

    This is the html

    <!DOCTYPE html>
    <html>
    <head>
    	<title>Check Video Exist in Youtube using PhP</title>
    	<style type="text/css">
    	.error{background:red;padding:5px;}
    	form{padding: 20px;}
    	</style>
    </head>
    <body>
    <h1>Check Youtube URL using PhP</h1>
    <form method="get">
    	<div><input type="text" name="video" id="video" value="" placeholder="Video Id"></div>
    	<div><input type="submit" id="btnsub" name="btnsub" value="Check..."></div>
    </form>
    </body>
    </html>
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: