22

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?

JohnThomas
  • 273
  • 1
  • 2
  • 10
  • 7
    change php.ini `allow_url_fopen=on` ;p – Lawrence Cherone Feb 10 '14 at 12:44
  • I've done this but it hasn't solved the problem. I have access to the server but I'm not to sure what to do from that end to turn it on – JohnThomas Feb 10 '14 at 12:52
  • 2
    dont forget to restart apache `sudo service apache2 restart` – Lawrence Cherone Feb 10 '14 at 13:07
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/), [Web Apps Stack Exchange](http://webapps.stackexchange.com/) or [Webmaster Stack Exchange](http://webmasters.stackexchange.com/) would be a better place to ask. – jww Jul 22 '16 at 12:03
  • As written, its a server config problem. I came across it while troubleshooting one of my server configs due to over-hardening with ModSecurity. Perhaps you should edit the question so that it is appropriate for this site. Or maybe, you can get the site's charter changed. – jww Nov 11 '16 at 18:49
  • The error message is server related but the cause is obviously PHP so this is indeed a programming question. This wrapper error still pops up a lot and frankly I forgot it was associated with a cURL call until reviewing this. And also lol Lawrence. – Ususipse Oct 03 '19 at 20:01

6 Answers6

22

@blytung Has a nice function to replace that function

<?php
$url = "http://www.example.org/";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($ch);
if (curl_errno($ch)) {
  echo curl_error($ch);
  echo "\n<br />";
  $contents = '';
} else {
  curl_close($ch);
}

if (!is_string($contents) || !strlen($contents)) {
echo "Failed to get contents.";
$contents = '';
}

echo $contents;
?>    
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
19

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.

Doug
  • 6,446
  • 9
  • 74
  • 107
11

Use this code in your php script (first lines)

ini_set('allow_url_fopen',1);
Ahmad
  • 507
  • 1
  • 11
  • 22
6

Edit your php.ini, find allow_url_fopen and set it to allow_url_fopen = 1

AndiPower
  • 853
  • 10
  • 20
0

Using relative instead of absolute file path solved the problem for me. I had the same issue and setting allow_url_fopen=on did not help. This means for instance :

use $file="folder/file.ext"; instead of $file="https://website.com/folder/file.ext"; in

$f=fopen($file,"r+");
-6

THIS IS A VERY SIMPLE PROBLEM

Here is the best method for solve this problem.

Step 1 : Login to your cPanel (http://website.com/cpanel OR http://cpanel.website.com).

Step 2 : SOFTWARE -> Select PHP Version

Step 3 : Change Your Current PHP version : 5.6

Step 3 : HIT 'Set as current' [ ENJOY ]

Sanjay Sikdar
  • 435
  • 4
  • 10
  • 2
    The only reason this might work is a different configuration file for that particular PHP version on your server. Changing you PHP version is not a justifiable solution when all you have to do is update the INI and restart Apache – Mavelo Apr 06 '18 at 21:10
  • well, thats the only logical solution here in this page. – Burhan Kashour Apr 28 '18 at 21:06
  • Exactly Mavelo. You have no way of knowing the configuration of that version either. – Ususipse Oct 03 '19 at 20:05