2012-12-14 158 views
2

我试图拉一个特定的YouTube频道的用户数。我介绍了一些关于Stackoverflow的链接以及外部网站,碰到类似this的链接。几乎所有的环节就建议我使用YouTube GDATA API和拉离subscriberCount计数,但下面的代码Youtube频道订阅人数

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

的print_r($ XML);

不返回这样的subscriberCount。是否有任何其他方式获得订阅人数或我做错了什么?

回答

8

试试这个;)

​​
+0

完美。谢谢:) – Dharma

+0

不客气;) – Zemistr

+3

此答案不再适用 - YouTube API 2.0已被弃用。我为v3添加了更新的答案。 – Ben

8

通过YouTube API 2.0版已被弃用。以下是如何使用3.0来完成的。 OAuth不是必需的。 1)登录Google帐户并转至https://console.developers.google.com/。您可能不得不开始一个新项目。

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

3)选择你需要(我用的浏览器应用程序“),这会给你一个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

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

+0

实际上完全适合我。编辑:我得到了错误的频道ID。 >。> – bitten

+0

完美!只是我需要它! –

5

我可以用正则表达式做到这一点对我的网页,不知道它为你工作或不。检查以下代码:

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

This Works !,谢谢!但任何想法如何做到这一点,当用户达到1000时,计数变成“1K”而不是全部计数? –

+0

@RhezJovovich你的意思是什么? – OnlyMAJ

+0

对不起,我的意思是,如果用户数为1200,则格式为1.2K。你有什么想法如何做到这一点? –

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

如果他们更新来源,这并不是真正有效的 – Jake

+0

如果他们更新来源,没有什么效果。 – rhoula

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