Youtube频道订阅者数量

时间:2012-12-14 02:28:33

标签: php youtube

我正在尝试为特定的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。有没有其他方法可以让订阅者知道或者我做错了什么?

5 个答案:

答案 0 :(得分:8)

试试这个;)

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

答案 1 :(得分:8)

不推荐使用YouTube API v2.0。以下是3.0如何做到这一点。不需要OAuth。

1)登录Google帐户,然后转到https://console.developers.google.com/。您可能必须开始一个新项目。

2)导航至APIs & auth并转到公开API访问 - &gt;创建新密钥

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

4)导航到YouTube中的频道,然后查看网址。频道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

&#34;试一试&#34;页面:https://developers.google.com/youtube/v3/docs/subscriptions/list#try-it

答案 2 :(得分:6)

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

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

答案 3 :(得分:3)

<?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";
}
?>

答案 4 :(得分:1)

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