使用 youtube api 显示subscriberCount - xmlns 解析
Posted
技术标签:
【中文标题】使用 youtube api 显示subscriberCount - xmlns 解析【英文标题】:Display subscriberCount using youtube api - xmlns parsing 【发布时间】:2012-01-15 19:57:54 【问题描述】:全部
我想使用它的 api 显示 youtube 订阅者。 但无法使用 php 获取subscriberCount。
这是 api 链接:http://gdata.youtube.com/feeds/api/users/worshipuk
结果
<?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'><id>http://gdata.youtube.com/feeds/api/users/worshipuk</id><published>2009-05-22T10:57:17.000-07:00</published><updated>2011-12-07T19:45:47.000-08:00</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#userProfile'/><category scheme='http://gdata.youtube.com/schemas/2007/channeltypes.cat' term='Musician'/><title type='text'>worshipuk</title><content type='text'>'Worship make brilliantly moody, haunting music. Soaring vocals and tense, synth-laden atmospherics combine with subtle rhythmic complexities, creating very mood driven, dark and broody sounds with bursts of light. A new project, the band spent a month of writing and recording (self-producing) at a small residential studio in Norway. A bright, if slightly dark future, awaits.' - RockFeedback
http://www.facebook.com/worshipuk
http://www.myspace.com/worshipuk
http://www.twitter.com/worshipuk</content><link rel='alternate' type='text/html' href='http://www.youtube.com/profile?user=worshipuk'/><link rel='http://gdata.youtube.com/schemas/2007#featured-video' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/cWZPWXWpa4I'/><link rel='related' type='text/html' href='http://www.worshipuk.com'/><link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/users/worshipuk'/><author><name>worshipuk</name><uri>http://gdata.youtube.com/feeds/api/users/worshipuk</uri></author><yt:age>110</yt:age><yt:description>'Worship make brilliantly moody, haunting music. Soaring vocals and tense, synth-laden atmospherics combine with subtle rhythmic complexities, creating very mood driven, dark and broody sounds with bursts of light. A new project, the band spent a month of writing and recording (self-producing) at a small residential studio in Norway. A bright, if slightly dark future, awaits.' - RockFeedback
http://www.facebook.com/worshipuk
http://www.myspace.com/worshipuk
http://www.twitter.com/worshipuk</yt:description><gd:feedLink rel='http://gdata.youtube.com/schemas/2007#user.favorites' href='http://gdata.youtube.com/feeds/api/users/worshipuk/favorites' countHint='2'/><gd:feedLink rel='http://gdata.youtube.com/schemas/2007#user.contacts' href='http://gdata.youtube.com/feeds/api/users/worshipuk/contacts' countHint='0'/><gd:feedLink rel='http://gdata.youtube.com/schemas/2007#user.inbox' href='http://gdata.youtube.com/feeds/api/users/worshipuk/inbox'/><gd:feedLink rel='http://gdata.youtube.com/schemas/2007#user.playlists' href='http://gdata.youtube.com/feeds/api/users/worshipuk/playlists'/><gd:feedLink rel='http://gdata.youtube.com/schemas/2007#user.subscriptions' href='http://gdata.youtube.com/feeds/api/users/worshipuk/subscriptions' countHint='0'/><gd:feedLink rel='http://gdata.youtube.com/schemas/2007#user.uploads' href='http://gdata.youtube.com/feeds/api/users/worshipuk/uploads' countHint='3'/><gd:feedLink rel='http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos' href='http://gdata.youtube.com/feeds/api/users/worshipuk/newsubscriptionvideos'/><yt:firstName>Worship</yt:firstName><yt:gender>m</yt:gender><yt:location>GB</yt:location><yt:statistics lastWebAccess='2011-11-10T03:33:40.000-08:00' subscriberCount='62' videoWatchCount='0' viewCount='1653' totalUploadViews='25141'/><media:thumbnail url='http://i1.ytimg.com/i/lwjfgavcsk4RwuI3yf0eJA/1.jpg?v=c8ce1f'/><yt:username>worshipuk</yt:username></entry>
我正在使用这个 php 代码来解析这个 xml 结果以获得subscriberCount:62
<?php
$det = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/worshipuk');
echo "<pre>";
print_r($det);
?>
如何使用 php 通过这个 api 获取subscriberCount?
【问题讨论】:
问题在哪里,问题出在哪里? @AwaisQarni 如何使用 php 使用此 api 获取subscriberCount ? 请输入print_r($det);
的结果
【参考方案1】:
我也有同样的问题。使用下面的代码我相信你会得到你的subscriberCount。
$name = '崇拜'; $url = 'http://gdata.youtube.com/feeds/api/users/'。 urlencode($name).'?alt=json'; $json = @file_get_contents($url); // 调皮的@-operator,使用正确的错误处理 $data = json_decode($json, TRUE); $count = (int) $data['entry']['yt$statistics']['subscriberCount']; 回声$计数;
【讨论】:
【参考方案2】:你也可以使用这个功能:
function get_yt_subs($username)
$xmlData = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . strtolower($username));
$xmlData = str_replace('yt:', 'yt', $xmlData);
$xml = new SimpleXMLElement($xmlData);
$subs = $xml->ytstatistics['subscriberCount'];
return($subs);
使用示例:
PHP 代码: get_yt_subs('GameTuts') 返回:
16733
【讨论】:
以上是关于使用 youtube api 显示subscriberCount - xmlns 解析的主要内容,如果未能解决你的问题,请参考以下文章
Youtube api 显示来自用户播放列表的有限视频 android
排除 youtube api 播放列表中显示“视频不可用”的所有视频