function emvideo_youtube_emvideo_generate in Asset 7
Implements hook_emvideo_generate().
File
- modules/
emvideo/ modules/ emvideo_youtube/ emvideo_youtube.module, line 37 - Emvideo youtube module.
Code
function emvideo_youtube_emvideo_generate() {
static $urls = array();
static $page = 0;
// If no urls are set, get a new page
if (count($urls) == 0) {
// Set pager
$page++;
$max_results = 50;
// Build url and make the request to Youtube API
$params = array(
'category' => 'Pets',
'fields' => 'entry(media:group(media:content(@url)))',
'max-results' => $max_results,
'start-index' => 1 + $max_results * ($page - 1),
);
$url = "https://gdata.youtube.com/feeds/api/videos?" . http_build_query($params);
$response = drupal_http_request($url);
if ($response->code == 200) {
try {
$xml = new SimpleXMLElement($response->data);
} catch (Exception $e) {
// SimpleXMLElement::__construct produces an E_WARNING error message for
// each error found in the XML data and throws an exception if errors
// were detected. Catch any exception and return failure (NULL).
return;
}
// If there is no valid project data, the XML is invalid, so return failure.
if (!isset($xml->entry)) {
return;
}
else {
// Parse the content and populate our array
foreach ($xml->entry as $entry) {
//All the media namespaced items under a YouTube video entry
$media = $entry
->children('media', TRUE);
$urls[] = strtok($media->group->content
->attributes()->url, '?');
}
}
}
}
// Get our value and remove it from the array
$key = array_rand($urls);
$url = $urls[$key];
unset($urls[$key]);
return $url;
}