Youtube 频道订阅人数

2022-01-06 00:00:00 youtube php

我正在尝试获取特定 YouTube 频道的订阅者数量.我参考了 Stackoverflow 和外部网站上的一些链接,遇到了类似 this.几乎所有链接都建议我使用 youtube gdata api 并从subscriberCount 中提取计数,但以下代码

I'm trying to pull the count of subscribers for a particular youtube channel. I referred some links on Stackoverflow as well as external sites, came across links like this. Almost all the links suggested me to use youtube gdata api and pull the count from subscriberCount but the following code

$data   = file_get_contents("http://gdata.youtube.com/feeds/api/users/Tollywood/playlists");
$xml    = simplexml_load_string($data);

print_r($xml);

print_r($xml);

不返回这样的subscriberCount.有没有其他方法可以让订阅者计数,或者我做错了什么?

returns no such subscriberCount. Is there any other way of getting subscribers count or am I doing something wrong?

推荐答案

试试这个 ;)

<?php 
$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/Tollywood');

$xml = new SimpleXMLElement($data);
$stats_data = (array)$xml->children('yt', true)->statistics->attributes();
$stats_data = $stats_data['@attributes'];

/********* OR **********/

$data = file_get_contents('http://gdata.youtube.com/feeds/api/users/Tollywood?alt=json');
$data = json_decode($data, true);
$stats_data = $data['entry']['yt$statistics'];

/**********************************************************/

echo 'lastWebAccess = '.$stats_data['lastWebAccess'].'<br />';
echo 'subscriberCount = '.$stats_data['subscriberCount'].'<br />';
echo 'videoWatchCount = '.$stats_data['videoWatchCount'].'<br />';
echo 'viewCount = '.$stats_data['viewCount'].'<br />';
echo 'totalUploadViews = '.$stats_data['totalUploadViews'].'<br />';
?>

相关文章