API youtube php, как узнать кол-во просмотров и продолжительность?
Здравствуйте, как узнать кол-во просмотров и продолжительность видео с youtube
Достаю xml http://gdata.youtube.com/feeds/api/videos/LtVdDWNMvIg
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .xml.geshi_code {font-family:monospace;} .xml.geshi_code .imp {font-weight: bold; color: red;} .xml.geshi_code .es0 {color: #000099; font-weight: bold;} .xml.geshi_code .br0 {color: #66cc66;} .xml.geshi_code .sy0 {color: #66cc66;} .xml.geshi_code .st0 {color: #ff0000;} .xml.geshi_code .sc-1 {color: #808080; font-style: italic;} .xml.geshi_code .sc0 {color: #00bbdd;} .xml.geshi_code .sc1 {color: #ddbb00;} .xml.geshi_code .sc2 {color: #339933;} .xml.geshi_code .sc3 {color: #009900;} .xml.geshi_code .re0 {color: #000066;} .xml.geshi_code .re1 {color: #000000; font-weight: bold;} .xml.geshi_code .re2 {color: #000000; font-weight: bold;} .xml.geshi_code span.xtra { display:block; }
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
.....
<media:group>
.....
<yt:duration seconds='938'/>
</media:group>
<yt:statistics favoriteCount='0' viewCount='296'/>
</entry>
Как мне достать viewCount и seconds?
Достаю xml http://gdata.youtube.com/feeds/api/videos/LtVdDWNMvIg
/** * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann * (http://qbnz.com/highlighter/ and http://geshi.org/) */ .xml.geshi_code {font-family:monospace;} .xml.geshi_code .imp {font-weight: bold; color: red;} .xml.geshi_code .es0 {color: #000099; font-weight: bold;} .xml.geshi_code .br0 {color: #66cc66;} .xml.geshi_code .sy0 {color: #66cc66;} .xml.geshi_code .st0 {color: #ff0000;} .xml.geshi_code .sc-1 {color: #808080; font-style: italic;} .xml.geshi_code .sc0 {color: #00bbdd;} .xml.geshi_code .sc1 {color: #ddbb00;} .xml.geshi_code .sc2 {color: #339933;} .xml.geshi_code .sc3 {color: #009900;} .xml.geshi_code .re0 {color: #000066;} .xml.geshi_code .re1 {color: #000000; font-weight: bold;} .xml.geshi_code .re2 {color: #000000; font-weight: bold;} .xml.geshi_code span.xtra { display:block; }
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
.....
<media:group>
.....
<yt:duration seconds='938'/>
</media:group>
<yt:statistics favoriteCount='0' viewCount='296'/>
</entry>
Как мне достать viewCount и seconds?
1 ответов
Таки сам наковырял что хотел
$apiURL = 'http://gdata.youtube.com/feeds/api/videos/'.$id_video;
$youtube = simplexml_load_file($apiURL);
$media = $youtube->children('http://search.yahoo.com/mrss/');
// кол-во просмотров
$yt = $youtube->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->statistics->attributes();
// прод-ть
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$attrs = $yt->duration->attributes();
$length = (int) $attrs['seconds'];
Как красивый вариант - заюзать Zend Framework:
$youtube = new Zend_Gdata_YouTube();
$video = $youtube->getVideoEntry('video_code');
echo $video->getVideoViewCount();
Один из возможных вариантов с помощью SimpleXml
$xml = file_get_contents('http://gdata.youtube.com/feeds/api/videos/LtVdDWNMvIg');
$entry = new SimpleXMLElement($xml);
// Продолжительность
$durationEl = reset($entry->xpath('media:group/yt:duration[@seconds]'));
$seconds = (int)$durationEl->attributes()->seconds;
// Кол-во просмотров
$statEl = reset($entry->xpath('yt:statistics[@viewCount]'));
$viewCount = (int)$statEl->attributes()->viewCount;