首页 文章

通过PHP中的JSON API获取Youtube订阅者数量

提问于
浏览
3

我想从此JSON文件中获取订阅者计数值:http://gdata.youtube.com/feeds/api/users/googlechrome?v=2&alt=json

这就是我所做的,但它不起作用 .

$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'];

2 回答

  • 4

    PHP代码:

    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('3moeslam')
    
  • 5

    我只是将JSON方法更改为XML,一切正常 . 我写的这个问题对@Matt Koskela有好处,但不适合我 . 无论如何,我将使用这种方法,但我真的想知道JSON方法的问题 .

    $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'];
    

相关问题