使用新的 YouTube API v3 解析 YouTube 订阅者计数

2024-03-20

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

  1. 我在这里为 youtube 创建了一个 Google API 应用程序:谷歌 API 控制台 https://code.google.com/apis/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;
?>

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

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。

EDIT:

我尝试使用 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;

?>

以下是 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用新的 YouTube API v3 解析 YouTube 订阅者计数 的相关文章

随机推荐

  • ASP.Net 使用什么 URL 重写器? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 为什么 android:fullBackupOnly 默认值是 false?

    In https developer android com guide topics manifest application element https developer android com guide topics manife
  • 如何避免链接多个 AsyncTask 调用?

    我必须对 Web 服务进行多次调用 但每个步骤都使用上一步中的值 因此现在我有一个巨大的 AsyncTasks 链 每个 AsyncTask 都在上一步的 AsyncTask 的 onPostExecute 中执行 这非常非常难看 而且很难
  • Perl DBIx::Class 可以覆盖从数据库检索列的方式吗?

    直到今天我才使用过 DBIx Class 所以我对它完全陌生 我不确定这是否可能 但基本上我的 SQLite 数据库中有一个表 其中有一个时间戳列 时间戳列的默认值为 CURRENT TIMESTAMP SQLite 将其存储在 GMT 时
  • 总是收到“消息”:“未经身份验证。” - Laravel 护照

    我一整天都找到了很多教程 我的设置与所有基本教程完全相同 目前 我可以访问http localhost oauth token成功地将令牌返回给我 之后 我使用 ARC Advanced Rest Client 来进行调用我自己的 api
  • 如何在SQL中获取2个表中不匹配的行?

    我有两个 SQL Server 表 CHANNELS SUBSCRIBERS 我想从中获取行CHANNELS不存在于SUBSCRIBERS在某种条件下 我尝试过INNER和OUTER LEFT JOIN但这对我不起作用 他们都给了我相同的答
  • 将一组字符串转换为 byte[] 数组

    我正在尝试将一组字符串转换为 byte 数组 首先 我执行以下操作将字节数组转换为字符串 public String convertByte byte msg String str for int i 0 i lt msg length i
  • 如何在iPhone中获取DNS服务器IP

    我尝试通过以下方式获取 etc resolv conf 打开 etc resolv conf 0644 但它返回 1并且errno是2这意味着 没有这样的文件 我能做些什么 您无法访问应用程序沙箱之外的文件
  • 反应本机错误 RCTJSONStringify() 遇到以下错误:JSON 写入中的类型无效 (NSURL)

    我正在尝试使用反应本机fbsdk在我的反应本机应用程序中 直到昨天为止都运行良好 但是 今天它给出了一个奇怪的错误RCTJSONStringify 遇到以下错误 JSON 写入 NSURL 中的类型无效 RN v0 42 0 这是我的代码
  • 从 dll 内的函数返回时堆损坏

    我有一个具有如下原型的函数 void function std string str 这个函数在另一个加载和使用该 dll 的程序的主函数中被调用 function some string value here 从该函数返回时 我收到堆损坏
  • 使用 Nokogiri 解析大型 HTML 文件

    我正在尝试解析与 Nokogiri 但不幸的是我无法从页面获取所有项目 我的简单测试代码是 require open uri require nokogiri html Nokogiri HTML open http www pro med
  • bash 中的视频方向检测

    我需要检测视频是以纵向还是横向模式录制的 然后以脚本方式将其转换为正确的方向 if v orient landscape then ffmpeg i file mp4 vf transpose 1 file ogv else ffmpeg
  • ABAP中调用方法的不同方式

    抱歉这个基本的 ABAP 问题 ABAP中调用方法有哪些不同的方式 他们的 官方 名字是什么 我听说过执行 方法调用和内部 内联方法调用 执行使用PERFORM关键字和方法调用CALL METHOD语法 我猜 但什么是 内部 或 内联方法调
  • 如何使用 std::cin 读取 bool

    我是 C 新手 我想知道函数 cin 在布尔数据的情况下如何工作 比方说 bool a cin gt gt a 我知道如果我给出 0 或 1 我的数据 a 将是 true 或 false 但是如果我给出另一个整数甚至一个字符串会发生什么 我
  • 允许所有用户进行临时分发查询

    我正在使用 AD Hoc 分布式查询将数据从 MS SQL Server 2008 传输到 MS Access 该过程使用单个 SQL 语句启动 INSERT INTO OpenDataSource Microsoft Jet OLEDB
  • Arrays.stream(array_name).sum() 比迭代方法慢吗?

    我正在编写一个 leetcode 问题 https oj leetcode com problems gas station https oj leetcode com problems gas station 使用Java 8 我的解决方
  • 是否可以将Spring Data JPA中的@Lock和@Modifying @Query与Hibernate结合起来?

    我有一个 Lock注解与 Modifying Query一起使用时出现问题和查询本身执行更新语句 我的测试设置如下所示 Spring Boot 启动器 1 5 3 RELEASE 休眠 5 2 10 Final Spring Data JP
  • Git 将存储库镜像到特定分支

    我们公司尝试将一个github项目fork到我们自己的git服务器上 然后我们可以在上面添加我们自己的功能 我们只想签出一个特定的分支 并将所有分支和标签保留到该分支 然后复制 镜像 到我们的 git 服务器 在您的服务器上创建存储库 在其
  • Pandas:迭代 DataFrame 列表并将每个数据框导出到 Excel 工作表

    尝试自学编码以自动化工作中一些繁琐的任务 对于任何无意的无知 我深表歉意 我在 pandas python 3 x 中创建了数据框 我想将每个数据框打印到不同的 Excel 工作表中 这是我的 2 个数据帧 它工作完美 但我想缩放它以循环遍
  • 使用新的 YouTube API v3 解析 YouTube 订阅者计数

    我想使用新的 API v3 从我的 YouTube 频道获取订阅者数量 我在这里为 youtube 创建了一个 Google API 应用程序 谷歌 API 控制台 https code google com apis console 我有