file_get_contents()“无法打开流:网络无法访问”错误的有效链接 [英] file_get_contents() "failed to open stream: Network is unreachable" error on valid link
问题描述
在PHP中使用Youtube API非常简单,但以某种方式无法正常工作,并且找不到明确的解决方案。
There's an extremely simple example of using Youtube API in php that somehow fails to work in my case, and I couldn't find a definitive solution for.
我希望使用Youtube API而无需任何包装,我自己就可以获取视频数据。下面的 search:list
查询在我尝试通过浏览器(作为链接)从内部访问它时非常有用,但是在php中,当我尝试使用相同的命令时,出现该错误。
I desire to use the Youtube API without any wrapper, by myself, to get data of videos. The following search:list
query works perfectly when I try accessing it from within by browser (as a link), but in php, I get that error when I try the same.
$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
,但在我的情况下似乎都没有帮助:前者实际上给出了一个新的错误消息:没有这样的文件或目录
。
The potential issues were URL encoding, or allowing allow-url-fopen
, but neither seemed to help in my case: the former actually gave a new error message: No such file or directory
.
我能做什么?
What can I do?
推荐答案
试试这个。可能是你的格式。
这适用于我。
Try it like this. Might be your formatting. This works for me.
$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>");
这篇关于file_get_contents()“无法打开流:网络无法访问”错误的有效链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!