3

i want to get the subscriber count value from this JSON file: http://gdata.youtube.com/feeds/api/users/googlechrome?v=2&alt=json

This is what i did but it's not working.

$youtube_url = json_decode( file_get_contents( 'http://gdata.youtube.com/feeds/api/users/googlechrome?v=2&alt=json' ), true );
$youtube_data = $youtube_url['entry']['yt$statistics']['subscriberCount'];
elkebirmed
  • 2,247
  • 6
  • 24
  • 35
  • This works fine for me. Can you describe what actually isn't working? – Matt Koskela Mar 02 '13 at 02:30
  • I know it should work fine. but not for me at least. maybe it wont work with some other codes in my whole script. anyway i figure out another method to do that. i will answer it bellow. thank you – elkebirmed Mar 02 '13 at 08:34

2 Answers2

5

PHP Code:

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('3moeslam')
Eslam Ahmad
  • 579
  • 7
  • 7
4

I just change the JSON method to XML and everything work fine for me. the question that i wrote work good for @Matt Koskela but not for me. anyway, i'll go a head with this method but i really want to know the problem with the JSON method.

$youtube_url = file_get_contents( 'http://gdata.youtube.com/feeds/api/users/googlechrome'; 
$youtube_url = str_replace( 'yt:', 'yt', $youtube_url ); 
$youtube_data = $youtube_url->ytstatistics['subscriberCount'];
elkebirmed
  • 2,247
  • 6
  • 24
  • 35