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.

 

Related Articles

If you like our content, please consider buying me a coffee.

Thank you for your support!

Buy me a coffeeBuy me a coffee

2 comments On Fix: file_get_contents() wrapper is disabled error in PHP

Leave a reply:

Your email address will not be published.

Site Footer