如何获得 Youtube 频道的订阅者数量?

Posted

技术标签:

【中文标题】如何获得 Youtube 频道的订阅者数量?【英文标题】:How to get number of subscribers to Youtube Channel? 【发布时间】:2015-09-27 11:52:06 【问题描述】:

我使用了这个代码...

$youtube_url = json_decode( file_get_contents( 'http://gdata.youtube.com/feeds/api/users/channelname?v=2&alt=json' ), true );
$youtube_data = $youtube_url['entry']['yt$statistics']['subscriberCount'];

...但是有一个错误:

“警告:file_get_contents(http://gdata.youtube.com/feeds/api/users/nikitaplay?v=2&alt=json) [function.file-get-contents]: 无法打开流:HTTP 请求失败!HTTP/1.0 410 Gone in...”

php不好,所以我不明白这个错误

更新: 我也使用了这段代码...

$youtube_url = json_decode( file_get_contents( 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id=d1chArLU0voGrMzbUM4G-A&key=AIzaSyCwAooaPaSzPEcDBULe-7jI3XZ6ZYRmiNY' ), true );

...但是有一个错误:

警告:file_get_contents(https://www.googleapis.com/youtube/v3/channels?part=statistics&id=d1chArLU0voGrMzbUM4G-A&key=AIzaSyCwAooaPaSzPEcDBULe-7jI3XZ6ZYRmiNY) [function.file-get-contents]:打开流失败:HTTP 请求失败! HTTP/1.0 403 Forbidden in...

【问题讨论】:

您好,欢迎来到 SO,请花点时间阅读Tour。由于您现在的问题是,它缺乏很多信息,包括出了什么问题,以及您遇到了什么错误,只是说“它不起作用”并不能告诉我们太多。因此,请尽可能多地为问题提供有用的信息来更新您的问题:) API v2 不再可用,使用 API v3 mpgn,我知道,这就是我问这个问题的原因 【参考方案1】:

1) 登录 Google 帐户并转到 https://console.developers.google.com/。您可能需要开始一个新项目。

2) 导航到 APIs & auth 并转到 Public API Access -> Create a New Key

3) 选择您需要的选项(我使用“浏览器应用程序”)这将为您提供 API 密钥。

4) 导航到您在 YouTube 中的频道并查看 URL。频道ID在这里:https://www.youtube.com/channel/YOUR_CHANNEL_ID

5) 使用 API 密钥和通道 ID 通过以下查询获取结果:https://www.googleapis.com/youtube/v3/channels?part=statistics&id=YOUR_CHANNEL_ID&key=YOUR_API_KEY

【讨论】:

但是有一个错误:警告:file_get_contents(googleapis.com/youtube/v3/…) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 禁止...【参考方案2】:

在https://console.developers.google.com中获取 Youtube v3 的 API 密钥

$apiKey = 'puthereyouapikey';
$chanelId = 'channelid';
$json = json_decode((file_get_contents('https://www.googleapis.com/youtube/v3/channels?part=statistics&id='.$channelId.'&key='.$apiKey)), true);
echo ($json != NULL) ? (($json['items']['0']['statistics']['subscriberCount'])) : '';

【讨论】:

以上是关于如何获得 Youtube 频道的订阅者数量?的主要内容,如果未能解决你的问题,请参考以下文章

如何获得YouTube频道的you-tube订阅者订阅列表?

使用新的 YouTube API v3 解析 YouTube 订阅者数量

使用新的 YouTube API v3 解析 YouTube 订阅者数量

Youtube 频道订阅人数

如何使用 YouTube Analytics API 获取任何 Youtube 频道的订阅用户列表及其详细信息?

如何确定用户是不是订阅了 youtube 频道