首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用youtube api - xmlns解析显示subscriberCount

使用youtube api - xmlns解析显示subscriberCount
EN

Stack Overflow用户
提问于 2011-12-08 11:09:07
回答 1查看 2.2K关注 0票数 3

我想用它的api显示youtube订阅者。但无法使用php获得subscriberCount。

这是api链接http://gdata.youtube.com/feeds/api/users/worshipuk

结果

代码语言:javascript
复制
    <?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结果以获得订阅计数:62。

代码语言:javascript
复制
<?php
$det = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/worshipuk');

echo "<pre>";
print_r($det);
?>

如何使用此api使用php获取subscriberCount?

EN

回答 1

Stack Overflow用户

发布于 2012-04-30 03:02:08

此外,您还可以使用此函数:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8430013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档