Skip to content

file_get_contents disabled on server, included a curl attempt before reporting error. #2

Closed
@Capeflair

Description

@Capeflair

If "allow_url_fopen" is not set on the server in php.ini then file_get_contents always returns null. I've included a new version executeDownload that I used to get around this issue. It uses the more secure curl function.

private function executeDownload($url) {
    if($this->cache_handler == null) {
        $data = @file_get_contents($url);
        if(!$data) {
            if (function_exists('curl_version')) {
                $curl = curl_init();
                 curl_setopt($curl, CURLOPT_URL, $url);
                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                 $data = curl_exec($curl);
                 curl_close($curl);              
                if(!$data) $this->error = "Invalid URL";
            } else {
                $this->error = "Invalid URL or check php.ini for allow_url_fopen";
            }
        }
    }
    else {
        $data = $this->attemptGetFromCache($url);
    }
    return $data;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions