访问 youtube api 时出错:“警告:file_get_contents() [function.file-get-contents]: URL > file-access is dis

Posted

技术标签:

【中文标题】访问 youtube api 时出错:“警告:file_get_contents() [function.file-get-contents]: URL > file-access is disabled...”【英文标题】:Error when accessing youtube api: 'Warning: file_get_contents() [function.file-get-contents]: URL > file-access is disabled...' 【发布时间】:2012-10-24 12:23:30 【问题描述】:

我试图遵循这个答案:

https://***.com/a/3331372/1063287

所以在我的 php 文档中,我有以下变量:

$json = file_get_contents("https://gdata.youtube.com/feeds/api/videos/$video_id?v=2&alt=json");
$json_data = json_decode($json);
$video_title = $json_data->'entry'->'title';
$video_date = $json_data->'entry'->'published';
$video_duration = $json_data->'entry'->'media:group'->'yt$duration';
$video_views = $json_data->'entry'->'yt$statistics'->'viewCount';
$video_description = $json_data->'entry'->'content';

但这给了我错误:

警告:file_get_contents() [function.file-get-contents]: URL 文件访问在服务器配置中被禁用 /home/path/to//file.php 在第 12 行

警告: file_get_contents(https://gdata.youtube.com/feeds/api/videos/xx-xxxxxxx?v=2&alt=json) [function.file-get-contents]:无法打开流:没有合适的 包装器可以在第 12 行的 /home/path/to/file.php 中找到

我在这里查看了开发人员指南:

https://developers.google.com/youtube/2.0/developers_guide_php

并且可以在这里了解提要和条目结构:

https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries

并找到了一些解决方案(一个涉及 curl),但它们要么没有工作,要么我不知道它们是否是最好的使用方法。

如果上面的代码有什么问题,谁能告诉我?

【问题讨论】:

检查 php.ini 中的 allow_url_fopen 是 ON 还是 OFF ***.com/questions/6551379/file-get-contents-error 看看这个。如果这可能会回答您的问题。 您也可以尝试使用 cURL 允许您检索数据。 谢谢大家,主机启用了 allow_url_fopen 和 openssl 并且该错误消息消失了,我现在有另一个,但我认为这是一个编码问题 Catchable fatal error: Object of class stdClass could not be converted to string。所以还有更多的功课要做,再次感谢你! 【参考方案1】:

这很可能是因为您的服务器上没有安装 OpenSSL。尝试安装它,如果仍然无法正常工作,可能是因为您在 php.ini 中禁用了 HTTP 访问(将 allow_url_fopen 设置为 true)

【讨论】:

【参考方案2】:

我看到的最好的东西是 Zend 框架中的Zend_GData_Youtube。它不需要任何特殊的东西,但它比普通的 cURL 或 JSON 请求要容易得多!

见:http://framework.zend.com/downloads/latest

【讨论】:

感谢您的信息,我还不熟悉zend,但我相信这个技巧会对某人有所帮助,谢谢!

以上是关于访问 youtube api 时出错:“警告:file_get_contents() [function.file-get-contents]: URL > file-access is dis的主要内容,如果未能解决你的问题,请参考以下文章

Youtube Data API V3 - 使用 google.youtube.videos.list() 获取视频时出错

Youtube Data API V3 - 使用google.youtube.videos.list()获取视频时出错

列出我的YouTube播放列表时出错

Nexus Player (Android TV) YouTubeAndroidPlayerApi 错误:“初始化 YouTube 播放器时出错。”

`Google Maps API 警告:NoApiKeys` 尝试访问 Youtube Data Api v3 时

YouTube API - 使用 Google 管理员帐户访问多个 youtube 频道(品牌帐户)