【问题标题】:file_get_contents() how to fix error "Failed to open stream", "No such file"file_get_contents() 如何修复错误“无法打开流”、“没有这样的文件”
【发布时间】:2013-12-31 23:28:05
【问题描述】:

当我尝试运行我的 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);

?>

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

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

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

【问题讨论】:

  • 8815 次浏览和 -2 评分?一定有人觉得这很有用。
  • 经常会遇到这个错误,要快速解决它,请按照以下步骤操作:stackoverflow.com/a/36577021/2873507

标签: php https


【解决方案1】:

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

【讨论】:

【解决方案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】:

你可以试试这个

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

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

【讨论】:

    【解决方案4】:

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

    $params2 = "some words";
    $params2 = urlencode($params2);
    
    $url = "http://abc/dgdc.php?p1=djkl&p2=$params2"
    
    $result = file_get_contents($url);
    

    【讨论】:

      【解决方案5】:

      只是通过简单的单元测试来扩展 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);
      }
      

      【讨论】:

        【解决方案6】:

        我们可以通过使用 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);
        

        【讨论】:

          【解决方案7】:

          这个错误的实际问题与file_get_content无关,问题是请求的url,如果url没有抛出页面内容并将请求重定向到其他地方file_get_content说“无法打开流”,只是在 file_get_contents 检查 url 是否正常工作并且没有重定向之前,这里是代码:

          函数 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;
          

          }

          【讨论】:

            【解决方案8】:

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

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

            用下面的查询替换

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

            【讨论】:

              猜你喜欢
              • 2016-12-20
              • 1970-01-01
              • 1970-01-01
              • 2017-06-20
              • 1970-01-01
              • 1970-01-01
              • 2011-12-14
              • 2015-02-25
              • 2019-07-27
              相关资源
              最近更新 更多