Last active
December 17, 2015 23:59
-
-
Save gerbenvandijk/5693404 to your computer and use it in GitHub Desktop.
// Function that returns random video ID from a users videofeed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to get all videos from a user?