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

标签 php xml wordpress

全部

我想使用它的 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?

最佳答案

我也有同样的问题。使用下面的代码我确定你得到了你的 subscriberCount。

$name = 'worshipuk'; 
$url   = 'http://gdata.youtube.com/feeds/api/users/'. urlencode($name).'?alt=json'; 
$json  = @file_get_contents($url); // Naughty @-operator, use proper error handling 
$data  = json_decode($json, TRUE); 
$count = (int) $data['entry']['yt$statistics']['subscriberCount']; 

echo $count;  

关于php - 使用 youtube api 显示 subscriberCount - xmlns 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8430013/

相关文章:

php - 根据商品数量有条件地按购物车商品添加折扣

php - SQL 中存储标签、类别和 int[] 的最佳方式

java - 安卓 : Parse XML with Sax failed (goes to exception)

regex - 如何使用 Perl 从 XML 中的多个根元素打印结果

css - 如何在导航栏后面贴一个背景?

WordPress 过滤器 user_row_actions

php - 评估 stripos(),!== FALSE 和 === TRUE 之间有什么区别?

php - 如何在 php 中保持 mysql 连接

php - 我有一个充满代码的数据库,我想在提交时调用这些数据

.net - LINQ to XML - 它是如何工作的?