php - file_get_contents() "failed to open stream: Network is unreachable"有效链接错误

标签 php https get youtube-api file-get-contents

有一个在 php 中使用 Youtube API 的非常简单的示例,但在我的情况下无法正常工作,我找不到明确的解决方案。

我希望自己使用不带任何包装器的 Youtube API 来获取视频数据。以下 search:list 查询在我尝试通过浏览器(作为链接)从内部访问它时完美运行,但在 php 中,当我尝试相同的操作时出现该错误。

        $apikey_YT = <my API key>;
        $ytrequrl = "https://www.googleapis.com/youtube/v3/search?".
                "part=snippet".
                "&maxResults=50".
                "&q=doom".
                "&relatedToVideoId=h8j2zj-A5tE".
                "&type=video".
                "&key=${apikey_YT}";

        $result = file_get_contents($ytrequrl);
        var_dump($result);

潜在的问题是 URL 编码,或允许 allow-url-fopen,但对我来说似乎都没有帮助:前者实际上给出了新的错误消息:No such file or目录

我能做什么?

最佳答案

试试看。可能是你的格式。 这对我有用。

$ytrequrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&q=doom&relatedToVideoId=h8j2zj-A5tE&type=video&key='.$apikey_YT;

$info= file_get_contents($ytrequrl);
$info= json_decode($info, true);

print("<pre>".print_r($info,true)."</pre>");

关于php - file_get_contents() "failed to open stream: Network is unreachable"有效链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43029811/

相关文章:

php - php文件中的8个空格如何使apache崩溃?

javascript - HTTPS 页面上的不安全内容

c# - 为什么 Request.Url.Scheme 不返回 HTTPS?

c# - Unity GET/POST 包装器

php - 在 PHP 中使用 data-urlencode 获取请求

php - 在重定向函数中使用变量

php - View 中的 Laravel undefined variable

git: 'remote-https' 不是 git 命令?

php - 在 View 中显示不必要的行