【问题标题】:Parse YouTube Subscribers Count with new YouTube API v3使用新的 YouTube API v3 解析 YouTube 订阅者数量
【发布时间】:2015-10-03 16:40:04
【问题描述】:

我想使用新的 API v3 从我的 youtube 频道获取订阅者数量。

  1. 我在这里为 youtube 创建了一个 Google API 应用程序:Google API Console
  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;
?>

即使代码 sn-p 看起来没问题,我也会遇到一些错误。

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。

编辑:

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

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

?>

【问题讨论】:

  • 这是你的文件ytsub2.php?如果是这样,我们能看到整个事情吗?
  • @jonmrich 这个 php 页面只包含将关注者数显示为测试页面的代码。
  • @cmorrissey 刚刚尝试使用 cURL,但我没有得到任何结果。请检查我的帖子编辑。谢谢
  • 你最后做对了吗?如果是,我需要那个代码!谢谢!

标签: php json youtube-api


【解决方案1】:

这是使用 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。

echo $ytsubscribers;

【讨论】:

    【解决方案2】:

    确保您在 Google Developers Console 中允许从您的 IP 地址进行访问。

    另外,更改这一行:

    $ytsubscribers = $ytcount['items'][0]['subscriberCount'];
    

    到这里:

    $ytsubscribers = $ytcount['items'][0]['statistics']['subscriberCount'];
    

    【讨论】:

    • 谢谢。在 Google Dev Console 中,此 API 允许每个 URL。你是对的“$ytsubscribers”是根据你所说的固定的。但它仍然没有显示任何内容。
    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2018-05-29
    相关资源
    最近更新 更多