Search code examples
phpwarnings

Server configuration by allow_url_fopen=0 in


I'm getting the following error when running a script. The error message is as follows...

Warning: file_get_contents() [function.file-get-contents]: https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/satoship/public_html/connect.php on line 22

I know this is a server issue but what do I need to do to the server in order to get rid of the above warning?


Solution

  • If you do not have the ability to modify your php.ini file, use cURL: PHP Curl And Cookies

    Here is an example function I created:

    function get_web_page( $url, $cookiesIn = '' ){
            $options = array(
                CURLOPT_RETURNTRANSFER => true,     // return web page
                CURLOPT_HEADER         => true,     //return headers in addition to content
                CURLOPT_FOLLOWLOCATION => true,     // follow redirects
                CURLOPT_ENCODING       => "",       // handle all encodings
                CURLOPT_AUTOREFERER    => true,     // set referer on redirect
                CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
                CURLOPT_TIMEOUT        => 120,      // timeout on response
                CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
                CURLINFO_HEADER_OUT    => true,
                CURLOPT_SSL_VERIFYPEER => true,     // Validate SSL Cert
                CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
                CURLOPT_COOKIE         => $cookiesIn
            );
    
            $ch      = curl_init( $url );
            curl_setopt_array( $ch, $options );
            $rough_content = curl_exec( $ch );
            $err     = curl_errno( $ch );
            $errmsg  = curl_error( $ch );
            $header  = curl_getinfo( $ch );
            curl_close( $ch );
    
            $header_content = substr($rough_content, 0, $header['header_size']);
            $body_content = trim(str_replace($header_content, '', $rough_content));
            $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; 
            preg_match_all($pattern, $header_content, $matches); 
            $cookiesOut = implode("; ", $matches['cookie']);
    
            $header['errno']   = $err;
            $header['errmsg']  = $errmsg;
            $header['headers']  = $header_content;
            $header['content'] = $body_content;
            $header['cookies'] = $cookiesOut;
        return $header;
    }
    

    NOTE: In revisiting this function I noticed that I had disabled SSL checks in this code. That is generally a BAD thing even though in my particular case the site I was using it on was local and was safe. As a result I've modified this code to have SSL checks on by default. If for some reason you need to change that, you can simply update the value for CURLOPT_SSL_VERIFYPEER, but I wanted the code to be secure by default if someone uses this.