使用新的YouTube API v3解析YouTube订阅者数量 [英] Parse YouTube Subscribers Count with new YouTube API v3

查看:82
本文介绍了使用新的YouTube API v3解析YouTube订阅者数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的API v3从我的youtube频道中获得订阅者人数.

I want to get the subscriber count from my youtube channel with the new API v3.

  1. 我在此处为youtube创建了一个Google API应用: Google API控制台
  2. 我同时拥有APP键和youtube频道ID
  3. 我使用"json_decode"来获取对象

<?php
    $url_yt = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNELID&key=APIKEY";
    $yt_array = file_get_contents($url_yt);
    $ytcount = json_decode($yt_array, true);
    $ytsubscribers = $ytcount['items'][0]['subscriberCount'];
     
    echo $ytsubscribers;
?>

即使代码段看起来还不错,我也会出现一些错误.

I get some errors even if the code snippet looks ok.

Warning: file_get_contents() [function.file-get-contents]: SSL: Success in /ytsub2.php on line 4

Warning: file_get_contents() [function.file-get-contents]: Failed to enable crypto in /ytsub2.php on line 4

Warning: file_get_contents(https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNELID&key=APIKEY) [function.file-get-contents]: failed to open stream: operation failed in /ytsub2.php on line 4

我不知道如何解决此错误.看来我的站点服务器无法正确获取JSON.

I have no idea how to fix this errors. It seems my site server is not able to get the JSON properly.

我尝试使用cURL却没有任何结果:

I tried using cURL without any result:

<?php

	//function to get the remote data
	function url_get_contents ($url) {
	    if (function_exists('curl_exec')){ 
	        $conn = curl_init($url);
	        curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true);
	        curl_setopt($conn, CURLOPT_FRESH_CONNECT,  true);
	        curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
	        $url_get_contents_data = (curl_exec($conn));
	        curl_close($conn);
	    }elseif(function_exists('file_get_contents')){
	        $url_get_contents_data = file_get_contents($url);
	    }elseif(function_exists('fopen') && function_exists('stream_get_contents')){
	        $handle = fopen ($url, "r");
	        $url_get_contents_data = stream_get_contents($handle);
	    }else{
	        $url_get_contents_data = false;
	    }
	return $url_get_contents_data;
	} 
	


	$url_yt = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNELID&key=APIKEY";
	$yt_array = url_get_contents($url_yt);
	$ytcount = json_decode($yt_array, true);
	$ytsubscribers = $ytcount['items'][0]['subscriberCount'];



// echo the youtube follower count
    echo $ytsubscribers;

?>

推荐答案

以下是使用cURL的正确工作代码:

Here is the correct working code with cURL:

	# url_get_contents function by Andy Langton: http://andylangton.co.uk/
	function url_get_contents($url,$useragent='cURL',$headers=false, $follow_redirects=false,$debug=false) {
	# initialise the CURL library
	$ch = curl_init();
	# specify the URL to be retrieved
	curl_setopt($ch, CURLOPT_URL,$url);
	# we want to get the contents of the URL and store it in a variable
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	# specify the useragent: this is a required courtesy to site owners
	curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
	# ignore SSL errors
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	# return headers as requested
	if ($headers==true){
	curl_setopt($ch, CURLOPT_HEADER,1);
	}
	# only return headers
	if ($headers=='headers only') {
	curl_setopt($ch, CURLOPT_NOBODY ,1);
	}
	# follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up
	if ($follow_redirects==true) {
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
	}
	# if debugging, return an array with CURL's debug info and the URL contents
	if ($debug==true) {
	$result['contents']=curl_exec($ch);
	$result['info']=curl_getinfo($ch);
	}
	# otherwise just return the contents as a variable
	else $result=curl_exec($ch);
	# free resources
	curl_close($ch);
	# send back the data
	return $result;
	}
 
 	$url_yt = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=YOUR_CHANNEL_ID&key=YOUR_API_KEY";
	url_get_contents($url_yt);
	$yt_array = url_get_contents($url_yt);
	$ytcount = json_decode($yt_array, true);
	$ytsubscribers = $ytcount['items'][0]['statistics']['subscriberCount'];

然后在要显示它的位置调用变量$ ytsubscribers.

Then call the variable $ytsubscribers where you want to display it.

echo $ytsubscribers;

这篇关于使用新的YouTube API v3解析YouTube订阅者数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆