【问题标题】:file_get_contents not working?file_get_contents 不工作?
【发布时间】:2011-12-09 07:48:08
【问题描述】:

此代码不适用于服务器。但它适用于我的本地主机(xampp)

$url = file_get_contents('http://www.site.com/');
$xhtml='|<tr style="background-color:#dddddd;">
        <td class="odd" align="left">(.+?)</td><td class="odd">(.+?)</td>
    </tr>|i';
preg_match_all($xhtml,$url,$score);
array_shift($score);
echo"<pre>";
print_r($score);
echo"</pre>";

当我像这样更改代码时,它会打印另一个分数。因为有两行这样的。它具有相同的代码。顺便说一下,下面的代码适用于服务器。

$xhtml='|<td class="odd" align="left">(.+?)</td><td class="odd">(.+?)</td>|i';

我需要在代码之间取这两个值。

allow_url_fopen = on

【问题讨论】:

  • php.ini 中是否“allow_url_fopen = On”?
  • 是的。 allow_url_fopen = on 第一个代码适用于 localhost 而不是服务器,第二个代码都适用。但我需要第一个代码。因为它提供了真实的分数。
  • 你确定 php cli 和 php apache2 php.ini 都允许allow_url_fopen
  • 在我的情况下,是 php 配置中的 chroot 指令导致了问题

标签: php regex file-get-contents


【解决方案1】:

如果 allow_url_fopenOn,则禁用您的 防火墙csf 并再次检查。

【讨论】:

    【解决方案2】:

    我们遇到了这个问题,结果证明这是不寻常的事情。我们试图 file_get_contents('https://www.google.com');我们的问题是因为服务器设置为使用 ipv6,但没有分配 ipv6 ip。我们禁用了 ipv6 并让它使用 ipv4 并且它工作。只是清单上的另一件事要检查。

    【讨论】:

      【解决方案3】:

      试试这个函数代替 file_get_contents():

      <?php
      
      function curl_get_contents($url)
      {
          $ch = curl_init();
      
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $url);
      
          $data = curl_exec($ch);
          curl_close($ch);
      
          return $data;
      }
      

      它可以像 file_get_contents() 一样使用,但使用 cURL。

      在 Ubuntu(或其他具有 aptitude 的类 unix 操作系统)上安装 cURL:

      sudo apt-get install php5-curl
      sudo /etc/init.d/apache2 restart
      

      另见cURL

      【讨论】:

      • 我的托管服务提供商 Netfirms 一定禁止了file_get_contents(),但这个功能在它的位置上起作用了。谢谢!
      【解决方案4】:

      我知道这个话题很老了,但我必须自己解决这个问题,并认为它会在以后对某人有所帮助。

      如上所说:

      你需要:

      允许打开网址 允许 url 包含

      如果你使用 CURL,那么你需要 curl 扩展

      如果你是 https:// 的 file_get_contents 我相信你还需要 apache ssl 模块,以及 openssl php 扩展。

      如果没有 OpenSSL 和 SSL 模块在 facebook 图(显然是 https://)上执行 file_get_contents,它会返回“未找到文件”错误。

      【讨论】:

      • 我遇到了同样的问题。 https 上的 file_get_contents:-> facebook graph API -> 返回 false。 CURL 解决了这个问题。
      • 我遇到了 https 问题...我认为您应该在答案中加粗以突出显示它。
      【解决方案5】:

      同时检查你是否有:allow_url_include On

      并确保不存在403 Forbidden之类的网络权限问题。

      【讨论】:

        【解决方案6】:

        你需要允许

         allow_url_fopen
        

        在您的 php.ini 配置文件中。一些主机出于安全考虑不允许这样做

        【讨论】:

          猜你喜欢
          • 2015-02-21
          • 1970-01-01
          • 1970-01-01
          • 2018-07-07
          • 1970-01-01
          • 2011-09-03
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          相关资源
          最近更新 更多