Reputation: 5418
all
I want to display youtube subscriber using its api. But not able to get subscriberCount using php.
this is the api link : http://gdata.youtube.com/feeds/api/users/worshipuk
Result
<?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>
I am using this php code to parse this xml result to get subscriberCount:62
<?php
$det = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/worshipuk');
echo "<pre>";
print_r($det);
?>
How to get subscriberCount with this api using php ?
Upvotes: 3
Views: 2209
Reputation: 11
Also you can use this function:
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);
}
Example of usage:
PHP Code:
get_yt_subs('GameTuts')
Returns:
16733
Upvotes: 1
Reputation: 310
I have same problem. use below code i am sure you get your 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;
Upvotes: 2