php Youtube 频道订阅人数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13871817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:12:35  来源:igfitidea点击:

Youtube channel subscriber count

phpyoutube

提问by Dharma

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

我正在尝试获取特定 youtube 频道的订阅者数量。我提到#2一些链接,以及外部站点,遇到像链接来。几乎所有的链接都建议我使用 youtube gdata api 并从subscriberCount 中提取计数,但以下代码

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

print_r($xml);

打印_r($xml);

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

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

回答by Ben

The YouTube API v2.0 is deprecated. Here's how to do it with 3.0. OAuth is not needed.

YouTube API v2.0 已弃用。以下是如何使用 3.0. 不需要 OAuth。

1) Log in to a Google account and go to https://console.developers.google.com/. You may have to start a new project.

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

2) Navigate to APIs & authand go to Public API Access -> Create a New Key

2) 导航到APIs & auth公共 API 访问 -> 创建新密钥

3) Choose the option you need (I used 'browser applications') This will give you an API key.

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

4) Navigate to your channel in YouTube and look at the URL. The channel ID is here: https://www.youtube.com/channel/YOUR_CHANNEL_ID

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

5) Use the API key and channel ID to get your result with this query: https://www.googleapis.com/youtube/v3/channels?part=statistics&id=YOUR_CHANNEL_ID&key=YOUR_API_KEY

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

Great success!

巨大的成功!



Documentation is actually pretty good, but there's a lot of it. Here's a couple of key links:

文档实际上非常好,但有很多。这里有几个关键链接:

Channel information documentation: https://developers.google.com/youtube/v3/sample_requests

频道信息文档:https: //developers.google.com/youtube/v3/sample_requests

"Try it" page: https://developers.google.com/youtube/v3/docs/subscriptions/list#try-it

“试用”页面:https: //developers.google.com/youtube/v3/docs/subscriptions/list#try-it

回答by Zemistr

Try this ;)

尝试这个 ;)

<?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 />';
?>

回答by OnlyMAJ

I could do it with regex for my page , not sure does it work for you or not . check following codes:

我可以用正则表达式为我的页面做这件事,不确定它是否适合你。检查以下代码:

<?php 
$channel = 'http://youtube.com/user/YOURUSERNAME/';
$t = file_get_contents($channel);
$pattern = '/yt-uix-tooltip" title="(.*)" tabindex/';
preg_match($pattern, $t, $matches, PREG_OFFSET_CAPTURE);
echo $matches[1][0];

回答by rhoula

<?php

//this code was written by Abdu ElRhoul
//If you have any questions please contact me at [email protected]
//My website is http://Oklahomies.com



set_time_limit(0);

function retrieveContent($url){
$file = fopen($url,"rb");
if (!$file)
    return "";
while (feof ($file)===false) {
    $line = fgets ($file, 1024);
    $salida .= $line;
}
fclose($file);
return $salida;
}

{
$content = retrieveContent("https://www.youtube.com/user/rhoula/about");         //replace rhoula with the channel name
$start = strpos($content,'<span class="about-stat"><b>');
$end = strpos($content,'</b>',$start+1);
$output = substr($content,$start,$end-$start);

echo "Number of Subscribers = $output";
}
?>

回答by user2499954

<?php 
echo get_subscriber("UCOshmVNmGce3iwozz55hpww");

function get_subscriber($channel,$use = "user") {
    (int) $subs = 0;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,            "https://www.youtube.com/".$use."/".$channel."/about?disable_polymer=1");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST,           0 ); 
    curl_setopt($ch, CURLOPT_REFERER, 'https://www.youtube.com/');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');
    $result = curl_exec($ch);
    $R = curl_getinfo($ch);
    if($R["http_code"] == 200) {
        $pattern = '/yt-uix-tooltip" title="(.*)" tabindex/';
        preg_match($pattern, $result, $matches, PREG_OFFSET_CAPTURE);
        $subs = intval(str_replace(',','',$matches[1][0]));
    }
    if($subs == 0 && $use == "user") return get_subscriber($channel,"channel");
    return $subs;
}