Random Youtube Video Selector

I took it upon myself to see if I could make a random video selector for youtube videos. The problem I am having is that every url that is randomly generated doesn’t exist on youtube. I’ve found that a video ID is eleven characters long, and contains alphanumeric characters plus the underscore and the hyphen. I have a method that makes such an ID, but when That ID is inserted into a URL, it never exists. The methods that find out if a video with x ID exists work because if I pass them a known Video ID, they say it does exist, and If I pass them garbage, they say it doesn’t exist. I tried a loop to call find_video over 100 times, but it still failed to find a video.

My question is, Why does the video that is randomly generated never exist?

Source code is below.

[code]

<?php

function random(){
$anarray = [“a”,“b”,“c”,“d”,“e”,“f”,“g”,“h”,“i”,“j”,“k”,“l”,“m”,“n”,“o”,“p”,“q”,“r”,“s”,“t”,“u”,“v”,“w”,“x”,“y”,“z”,“A”,“B”,“C”,“D”,“E”,“F”,“G”,“H”,“I”,“J”,“K”,“L”,“M”,“N”,“O”,“P”,“Q”,“R”,“S”,“T”,“U”,“V”,“W”,“X”,“Y”,“Z”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“0”,"_","-"];

$num = rand(0, 63); 
$url = array($anarray[$num]);
        
return $url[0];    
}

function get_http_response_code($theURL) {
$headers = get_headers($theURL);
return substr($headers[0], 9, 3);
}

function getvid(){
$st;
$vid = ([]);

for($i = 0; $i <= 10; $i++){
    array_push($vid, random());
}

$st = implode("|", $vid);
$good = str_replace("|", "", $st);
return $good;

}

function does_vid_exist($vid){
$videoID = $vid;
$string = “http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoID&format=json”;

$code = get_http_response_code ($string);

if ($code == "404") {
    return false;
} else {
    return true;
}}

function find_video($v){
$vid = $v;
if (does_vid_exist($vid) == true)
{
return $vid;
} else {
return “Video Not Found”;
}
}

?>

Random Youtube Video
<style type="text/css">

::selection{ background-color: #E13300; color: white; }
::moz-selection{ background-color: #E13300; color: white; }
::webkit-selection{ background-color: #E13300; color: white; }

body {
	background-color: #fff;
	margin: 40px;
	font: 13px/20px normal Helvetica, Arial, sans-serif;
	color: #4F5155;
}

a {
	color: #003399;
	background-color: transparent;
	font-weight: normal;
}

h1 {
	color: #444;
	background-color: transparent;
	border-bottom: 1px solid #D0D0D0;
	font-size: 19px;
	font-weight: normal;
	margin: 0 0 14px 0;
	padding: 14px 15px 10px 15px;
}

code {
	font-family: Consolas, Monaco, Courier New, Courier, monospace;
	font-size: 12px;
	background-color: #f9f9f9;
	border: 1px solid #D0D0D0;
	color: #002166;
	display: block;
	margin: 14px 0 14px 0;
	padding: 12px 10px 12px 10px;
}

#body{
	margin: 0 15px 0 15px;
}

p.footer{
	text-align: right;
	font-size: 11px;
	border-top: 1px solid #D0D0D0;
	line-height: 32px;
	padding: 0 10px 0 10px;
	margin: 20px 0 0 0;
}

#container{
	margin: 10px;
	border: 1px solid #D0D0D0;
	-webkit-box-shadow: 0 0 8px #D0D0D0;
}
</style>
<div id="container">
    <iframe width="1280" height="720" src="//www.youtube.com/embed/<?php echo getvid();   
                ?>"frameborder="0" allowfullscreen></iframe>
    <iframe width="1280" height="720" src="//www.youtube.com/embed/<?php echo find_video("RkkAgnnBdIE"); 
                ?> "frameborder="0" allowfullscreen></iframe>
    <iframe width="1280" height="720" src="//www.youtube.com/embed/<?php echo find_video("Not a Video ID"); 
                ?> "frameborder="0" allowfullscreen></iframe>
[/code]

Have you tried checking to see if any of the generated urls really exist?

There have to be lots of unused urls, or that would me that no more could be added.

Better to collect know existing, from playlists or something.

Sponsor our Newsletter | Privacy Policy | Terms of Service