gpt4 book ai didi

php - file_get_contents() 如何修复错误 "Failed to open stream", "No such file"

转载 作者:IT王子 更新时间:2023-10-28 23:49:59 25 4
gpt4 key购买 nike

当我尝试运行我的 PHP 脚本时出现以下错误:

failed to open stream: No such file or directory in C:\wamp\www\LOF\Data.php on line 3 script:

我的代码如下:

<?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);

?>

注意:*key* 是 URL 中字符串(我的 API key )的替代品,出于隐私原因已被隐藏。

我从 URL 中删除了 https:// 以使一个错误消失。

我是不是做错了什么?也许是网址?

最佳答案

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

您需要在尝试从中获取内容的 URL 的开头添加 httphttps:

$json = json_decode(file_get_contents('http://...'));

至于如下错误:

Unable to find the wrapper - did you forget to enable it when you configured 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;
}

用法:

$url = 'https://...';
$json = json_decode(curl_get_contents($url));

关于php - file_get_contents() 如何修复错误 "Failed to open stream", "No such file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com