file_get_contents()如何修复错误"无法打开流","没有这样的文件"

Lif*_*ess 25 php https

我尝试运行PHP脚本时收到以下错误:

无法打开流:第3行脚本中的C:\ wamp\www\LOF\Data.php中没有此类文件或目录:

我的代码如下:

<?php

$json = json_decode(file_get_contents('prod.api.pvp.net/api/lol/euw/v1.1/game/by-summoner/20986461/recent?api_key=*key*'));

print_r($json);

?>
Run Code Online (Sandbox Code Playgroud)

注意:*key*是URL(我的API密钥)中字符串的替换,并且出于隐私原因而被隐藏.

https://从URL中删除了一个错误消失.

我在这里做错了吗?也许是URL?

Ama*_*ali 32

URL缺少协议信息.PHP认为它是一个文件系统路径,并尝试访问指定位置的文件.但是,该位置实际上并不存在于您的文件系统中,并且会引发错误.

您需要添加httphttps在URL的开头添加以下内容:

$json = json_decode(file_get_contents('http://...'));
Run Code Online (Sandbox Code Playgroud)

至于以下错误:

无法找到包装器 - 您是否忘记在配置PHP时启用它?

您的Apache安装可能未使用SSL支持进行编译.您可以手动尝试安装OpenSSL并使用它,或使用cURL.我个人更喜欢cURL file_get_contents().这是您可以使用的功能:

function curl_get_contents($url)
{
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
Run Code Online (Sandbox Code Playgroud)

用法:

$url = 'https://...';
$json = json_decode(curl_get_contents($url));
Run Code Online (Sandbox Code Playgroud)