【问题标题】:Youtube channel subscriber countYoutube 频道订阅人数
【发布时间】:2012-12-02 01:11:15
【问题描述】:

我正在尝试获取特定 youtube 频道的订阅者数量。我提到了 Stackoverflow 和外部网站上的一些链接,遇到了像 this 这样的链接。几乎所有链接都建议我使用 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);

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

【问题讨论】:

    标签: php youtube


    【解决方案1】:

    试试这个;)

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

    【讨论】:

    • 完美运行。谢谢:)
    • 这个答案不再有效 - YouTube API 2.0 已被弃用。我为 v3 添加了一个更新的答案。
    【解决方案2】:

    YouTube API v2.0 已弃用。这是使用 3.0 的方法。不需要 OAuth。

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

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

    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

    大获成功!


    文档实际上相当不错,但其中有很多。以下是几个关键链接:

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

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

    【讨论】:

    • 实际上非常适合我。编辑:我得到了错误的频道 ID。 >.>
    • 完美!我只需要它!
    【解决方案3】:

    我可以为我的页面使用正则表达式,但不确定它是否适合您。检查以下代码:

    <?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];
    

    【讨论】:

    • 这行得通!谢谢!但是当订阅者达到 1000 时,知道如何做到这一点,计数变为“1K”而不是全部计数?
    • @RhezJovovich 你到底是什么意思?
    • 对不起,我的意思是如果订阅者数量为 1200 则格式化为 1.2K 。你知道怎么做吗?
    • 无论如何,请忽略我关于格式的问题,我已经从现有的 stackoverflow 帖子中找到了答案。现在,我的问题是,“$matches[1][0]”值是返回字符串还是数字?非常感谢!
    • @RhezJovovich 绝对是字符串,但如果您使用 intval 函数,它将返回数字。您可以使用简单的代码来进行格式化,例如echo ((intval)$matches[1][0]&gt;1000?(intval)$matches[1][0]/1000).'K':(intval)$matches[1][0]
    【解决方案4】:
    <?php
    
    //this code was written by Abdu ElRhoul
    //If you have any questions please contact me at info@oklahomies.com
    //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";
    }
    ?>
    

    【讨论】:

    • 如果他们更新他们的源代码,这并不是很有效
    • 如果他们更新源代码,实际上没有什么是有效的。
    【解决方案5】:
    <?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;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2014-07-15
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多