file_get_contents()如何修复错误“Failed to open stream”,“No such file”

42

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

在C:\wamp\www\LOF\Data.php的第3行,打开流失败:没有这样的文件或目录 脚本:

我的代码如下:

<?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密钥),因隐私原因已隐藏。

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

我在这里做错了什么吗?也许是URL的问题?


3
8815次浏览和-2的评分?一定有人认为这很有用。 - easymoden00b
经常会遇到这个错误,为了快速排除故障,请按照以下步骤操作:https://dev59.com/LVoV5IYBdhLWcg3wL8Ww#36577021 - Vic Seedoubleyew
8个回答

47

该URL缺少协议信息。PHP认为它是一个文件系统路径,并尝试访问指定位置的文件。然而,该位置在您的文件系统中实际上不存在,因此会抛出错误。

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

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

关于以下错误:

无法找到包装器 - 您在配置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));

1
现在有一个针对这个常见错误的故障排除清单,链接在这里:https://dev59.com/LVoV5IYBdhLWcg3wL8Ww#36577021 - Vic Seedoubleyew

2
你可以尝试使用这个。
<?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);
?>

"./"允许从当前目录搜索url。 您可以使用


chdir($_SERVER["DOCUMENT_ROOT"]);

将当前工作目录更改为您网站的根目录,如果路径相对于根目录。

2
为什么不使用cURL
$yourkey="your api key";
$url="https://prod.api.pvp.net/api/lol/euw/v1.1/game/by-summoner/20986461/recent?api_key=$yourkey";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$auth = curl_exec($curl);
if($auth)
{
$json = json_decode($auth); 
print_r($json);
}
}

3
我不知道什么是 cUrl,我对 PHP 还比较新,请原谅。 - Life of Madness
这只是PHP的一个扩展,你需要启用它。很可能默认情况下已经启用了。无论如何,你可以在这里看看… http://www.tomjepson.co.uk/enabling-curl-in-php-php-ini-wamp-xamp-ubuntu/ - Shankar Narayana Damodaran

1

我只需要通过在URL中编码参数来解决这个问题。
URL可能是:http://abc/dgdc.php?p1=Hello&p2=some words
我们只需要对参数2进行编码即可。

$params2 = "some words";
$params2 = urlencode($params2);

$url = "http://abc/dgdc.php?p1=djkl&p2=$params2"

$result = file_get_contents($url);

0
只是要通过简单的单元测试来扩展Shankars和amals的答案:
/**
 *
 * workaround HTTPS problems with file_get_contents
 *
 * @param $url
 * @return boolean|string
 */
function curl_get_contents($url)
{
    $data = FALSE;
    if (filter_var($url, FILTER_VALIDATE_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;
}
// then in the unit tests:
public function test_curl_get_contents()
{
    $this->assertFalse(curl_get_contents(NULL));
    $this->assertFalse(curl_get_contents('foo'));
    $this->assertTrue(strlen(curl_get_contents('https://www.google.com')) > 0);
}

0
我们可以通过使用Curl来解决这个问题...
function my_curl_fun($url) {
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 $data = curl_exec($ch);
 curl_close($ch);
 return $data;
}

$feed = 'http://................'; /* Insert URL here */
$data = my_curl_fun($feed);

0

这个错误的实际问题与file_get_content无关,问题在于请求的url。如果url没有返回页面内容并将请求重定向到其他地方,file_get_content会显示“打开流失败”。在使用file_get_contents之前,请检查url是否有效且未重定向。以下是代码:

function checkRedirect404($url) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_URL, $url);
    $out = curl_exec($ch);

    // line endings is the wonkiest piece of this whole thing
    $out = str_replace("\r", "", $out);

    // only look at the headers
    $headers_end = strpos($out, "\n\n");
    if( $headers_end !== false ) { 
        $out = substr($out, 0, $headers_end);
    }   

    $headers = explode("\n", $out);
    foreach($headers as $header) {
        if( substr($header, 0, 10) == "Location: " ) { 
            $target = substr($header, 10);

            //echo "Redirects: $target<br>";

            return true;
        }   
    }   

    return false;

}


-1

希望以下解决方案能对大家有用,因为我在我的网站上遇到了同样的问题...

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

请使用以下查询替换:

$Details= unserialize(file_get_contents('http://......'));


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接