Fix: file_get_contents() wrapper is disabled error in PHP

If you are are trying to use file_get_contents() function to fetch the contents of a URL and you receive Error something like

Warning: file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0

Then you have following solutions

  1. Enable the allow_url_fopen parameter in your php.ini configuration. Although this is an easy approach this is not recommended because of the security holes it opens.
  2. Second way is to replace your file_get_contents call in your code with CURL. This is a simple CURL method that behaves exactly same as file_get_contents.
function curl_get_file_contents($URL)
    {
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
        else return FALSE;
    }

This simple method returns the content of the URL which is passed in as the parameter and returns FALSE is there is no content available.

 

tgugnani: Web Stuff Enthusiast.