【问题标题】:File_get_contents is not defined error in Apache serverFile_get_contents 在 Apache 服务器中未定义错误
【发布时间】:2015-04-07 18:24:36
【问题描述】:

我编写了一个简单的脚本来获取页面的内容。 当我在 Apache 服务器上运行它时,我收到一个错误:

file_get_contents 未定义

Apache 2.2、PHP 5.3.9。代码如下:

$url = "http://steamcommunity.com/market/priceoverview/?appid=730&country=PL&currency=3&market_hash_name=Desert%20Eagle%20%7C%20Blaze%20(Minimal%20Wear)";
$json = file_get_contents($url);
$price = json_decode($json);
alert(price.lowest_price);

如何解决?

【问题讨论】:

  • 嗯,什么? ........这似乎不可能?
  • 有可能。你是在本地运行吗?
  • @unixmiah 是的,我是。

标签: php json apache


【解决方案1】:

如果您是本地人,这可能是个问题。在您的 php.ini 文件中尝试添加

allow_url_fopen = 1

然后重试。如果我必须抓取文件,我喜欢使用 cURL。这是远程抓取文件的非常简单的示例。

/* gets the data from a URL */
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

用法

$returned_content = get_data('http://www.grab.com/grab-me.html');

无论您是否遇到 file_get_contents 问题,这都能保证工作

【讨论】:

  • 你在用什么? xampp 还是 wamp?它在窗户上吗?你的灯是怎么设置的?
  • "allow_url_fopen = 1" 没有帮助。我应该在在线服务器上运行它吗?当我尝试运行您的 cURL 示例时,出现错误: curl_init 未定义。我真的很想在没有库的情况下完成这项工作。这可能吗?
  • 我只是将 Apache 与 PHP 一起使用,而不是 XAMPP 或 WAMP。
猜你喜欢
  • 2018-02-18
  • 2012-08-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2014-06-08
  • 2020-03-16
  • 2014-11-26
相关资源
最近更新 更多