【问题标题】:file_get_contents( ) not workingfile_get_contents() 不工作
【发布时间】:2015-02-21 04:28:31
【问题描述】:

我有一个脚本,它使用file_get_contents() 从远程服务器获取 json 响应。 虽然 file_get_contents() 在本地文件上正常工作,但不能使用 http 或 https 它给我以下错误 file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 infile_get_contents(https://api.domain.com/resolve.json?url=blablabla): failed to open stream: no suitable wrapper could be found .. 它是一个专用服务器,我有 WHM .. 我试过了

  1. 在 WHM PHP 配置编辑器上设置 allow_url_fopen = on 但这不起作用。
  2. 在发生错误的目录中创建一个带有allow_url_fopen = on 的php.ini 文件,但这不起作用。
  3. 在 PHP 脚本的开头添加了ini_set('allow_url_fopen', 'On');,但这不起作用。

我知道我可以改用 Curl,但我想知道为什么这不起作用.. 脚本在其他服务器和 localhost 上正常工作

更新:

phpinfo();

给我

allow_url_fopen Off 
allow_url_include   Off 
always_populate_raw_post_data   Off

这意味着我的 php.ini 更改没有生效.. 我做错了什么?

【问题讨论】:

  • allow_url_fopen 可能已被覆盖,因此您无法打开它。即使使用您的 php.ini 等,运行 phpinfo() 并查看它是否真的被更改了。我的建议是使用 curl,fwiw。
  • /etc/中编辑php.ini,重启网络服务器service httpd restart

标签: php apache curl


【解决方案1】:

除了上述答案之外,SELinux 设置可能不允许您从 httpd 进行连接。为此,您可以参考此链接 https://drib.tech/programming/php-file_get_contents-not-working

它对我有用

【讨论】:

    【解决方案2】:

    这项工作完美:

    function get_site($url) { 
      $file = fopen($url, 'r');
      if(!$file) return;
      $buffer='';
      while(!feof($file))   {
        $buffer .= fgets($file, 4096);
        }
      fclose($file);
      return $buffer;
    }
    

    【讨论】:

      【解决方案3】:

      使用 curl 作为 file_get_contents( ) 的替代,这是我正在使用的函数

      function url_get_contents ($Url) {
          if (!function_exists('curl_init')){ 
              die('CURL is not installed!');
          }
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $Url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $output = curl_exec($ch);
          curl_close($ch);
          return $output;
      }
      

      【讨论】:

        【解决方案4】:
        $id = $_GET['id'] ;
        $api_url = "http://localhost/../API.php?id='$id' ";
        $port = 80;
        $ch = curl_init( );
        curl_setopt ( $ch, CURLOPT_URL, $api_url );
        curl_setopt ( $ch, CURLOPT_PORT, $port );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        // Allowing cUrl funtions 20 second to execute
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 );
        // Waiting 20 seconds while trying to connect
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 5 );                                 
        $response_string = curl_exec( $ch );
        
        echo $response_string;
        

        这是解决网络请求问题的最佳方式

        【讨论】:

          【解决方案5】:
          $url= 'https://example.com';
          
          $arrContextOptions=array(
                "ssl"=>array(
                      "verify_peer"=>false,
                      "verify_peer_name"=>false,
                  ),
              );  
          
          $response = file_get_contents($url, false, stream_context_create($arrContextOptions));
          

          这将允许您从 url 获取内容,无论是 HTTPS,无需更改 php ini。

          【讨论】:

          • 谢谢。这个额外的配置正是解决了我们的问题的原因。我在另一个问题上写了一个答案(关注问题的突然性)。我引用了你的答案作为我的来源:stackoverflow.com/a/48305167/1385429
          • 不同意,MPDF 也使用 file_get_contents 和许多其他库,你是不是疯了建议更改为他们的核心代码??
          【解决方案6】:

          如果您有 root 访问权限,请编辑 php.ini,通常位于 /etc/php.ini

          如果您没有root权限,请尝试添加ini_set('allow_url_fopen', 'On');ini_set('allow_url_fopen', '1');

          如果您看不到 php.ini,请尝试在 PHP 脚本中使用 phpinfo() 来查找 php.ini 位置。

          【讨论】:

            【解决方案7】:

            通过 ssh 登录到您的服务器并输入

            sudo nano /etc/php5/apache2/php.ini //<<<< ubuntu/debian server, might differ for you
            

            在文件中,只需按"ctrl + w" 并输入"allow_url_fopen" 并回车,很可能您会先来解释,所以重复搜索几次。现在您可以从

            更改条目

            allow_url_fopen=0

            allow_url_fopen=1

            "ctrl + x" 并使用"y" 确认文件保存。

            然后输入

            sudo service apache2 restart
            

            这将重新启动 apache,以便可以加载新的 php.ini 配置。完成这些步骤后,您应该可以在外部使用file_get_contents


            旁注

            如果您找不到您的 php.ini 文件,您可以在phpinfo() 的顶部找到加载的 php.ini 文件的路径

            【讨论】:

            • 谢谢 .. 我忘了检查加载的配置文件 .. 成功了 .. 我有一个旧的 php.ini public_html 文件夹 .. 但子文件夹中的文件不应该覆盖它吗?
            • 我放置新 php.ini 的子目录中的文件
            • 嗯,我认为主要加载的配置文件将始终在上述位置。
            猜你喜欢
            • 2011-12-09
            • 1970-01-01
            • 1970-01-01
            • 2018-07-07
            • 1970-01-01
            • 2011-09-03
            • 1970-01-01
            • 1970-01-01
            • 2023-04-05
            相关资源
            最近更新 更多