Nel tutorial di oggi vedremo come creare uno script che da un’ID YouTube riuscirà ad ottenere tutte le informazioni utili del video come ad esempio titolo, descrizione, durata, url miniatura, larghezza e altezza miniatura e keywords.

 
< ?php
//The Youtube’s API url
define(‘YT_API_URL’, ‘http://gdata.youtube.com/feeds/api/videos?q=’);
 
//change below the video id.
$video_id = "ID_YOUTUBE";
 
//Using cURL php extension to make the request to youtube API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, YT_API_URL . $video_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//$feed holds a rss feed xml returned by youtube API
$feed = curl_exec($ch);
curl_close($ch);
 
//Using SimpleXML to parse youtube’s feed
$xml = simplexml_load_string($feed);
$entry = $xml->entry[0];
$media = $entry->children(‘media’, true);
$group = $media[0];
 
$title = $group->title;//$title: The video title
$desc = $group->description;//$desc: The video description
$vid_keywords = $group->keywords;//$vid_keywords: The video keywords
$thumb = $group->thumbnail[0];//There are 4 thumbnails, The first is the largest.
//$thumb_url: the url of the thumbnail. $thumb_width: thumbnail width in pixels.
//$thumb_height: thumbnail height in pixels. $thumb_time: thumbnail time in the vi�­deo
list($thumb_url, $thumb_width, $thumb_height, $thumb_time) = $thumb->attributes();
$content_attributes = $group->content->attributes();
//$vid_duration: the duration of the video in seconds. Ex.: 192.
$vid_duration = $content_attributes[‘duration’];
//$duration_formatted: the duration of the video formatted in "mm:ss". Ex.:01:54
$duration_formatted = str_pad(floor($vid_duration/60), 2, ’0′, STR_PAD_LEFT) . ‘:’ . str_pad($vid_duration%60, 2, ’0′, STR_PAD_LEFT);
 
//echoing the variables for testing purposes:
echo ‘title: ‘ . $title . ;
echo ‘desc: ‘ . $desc . ;
echo ‘video keywords: ‘ . $vid_keywords . ;
echo ‘thumbnail url: ‘ . $thumb_url . ;
echo ‘thumbnail width: ‘ . $thumb_width . ;
echo ‘thumbnail height: ‘ . $thumb_height . ;
echo ‘thumbnail time: ‘ . $thumb_time . ;
echo ‘video duration: ‘ . $vid_duration . ;
echo ‘video duration formatted: ‘ . $duration_formatted;
?>
 

fonte: www.sastgroup.com ? Vai al post originale