Skip to content

Instantly share code, notes, and snippets.

@gerbenvandijk
Last active December 17, 2015 23:59
Show Gist options
  • Save gerbenvandijk/5693404 to your computer and use it in GitHub Desktop.
Save gerbenvandijk/5693404 to your computer and use it in GitHub Desktop.
// Function that returns random video ID from a users videofeed
<?php
// Function that returns random video ID from a users videofeed
function RandomYoutube($youtubefeed){
$feedresult = simplexml_load_file($youtubefeed);
$videoids = array();
foreach ($feedresult->entry as $video) {
$media = $video->children('media', true);
$url = (string)$media->group->player->attributes()->url;
$index = strrpos($url, "&");
$url = substr($url, 0, $index);
$index = strrpos($url, "watch");
$url = substr($url, $index + 8, strlen($url) - ($index + 8));
$videoids[] = $url;
}
return $videoids[array_rand($videoids)];
}
// Usage (not tested with other feed format, just replace username with your username)
echo RandomYoutube('http://gdata.youtube.com/feeds/api/users/username/uploads');
?>
@rkaartikeyan
Copy link

How to get all videos from a user?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment