2015-04-07 18:24


I wrote a simple script to get content of a page. When I am running it on Apache server, I'm getting an error:

file_get_contents is not defined

Apache 2.2, PHP 5.3.9. Here is the code:

$url = "";
$json = file_get_contents($url);
$price = json_decode($json);

How to fix it?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duancilan5124 duancilan5124 6年前

    If you're local that may be an issue. In your php.ini file try to add

    allow_url_fopen = 1

    and re-try. I like to use cURL if I have to grab a file. Here is really simple example of file grabbing remotely.

    /* 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);
        return $data;

    The Usage

    $returned_content = get_data('');

    This is guaranteed to work whether or not you're having issues with file_get_contents

    点赞 评论 复制链接分享