Warning: File_Get_Contents(): Https:// Wrapper Is Disabled in the Server Configuration by All

Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by all

First, check your PHP file with this code and then enable the fopen in your php.ini file

<?php 
if( ini_get('allow_url_fopen') ) {
die('allow_url_fopen is enabled. file_get_contents should work well');
} else {
die('allow_url_fopen is disabled. file_get_contents would not work');
}

?>

Edit the php.ini file and enable using below code

allow_url_fopen = 1 //0 for Off and 1 for On Flag
allow_url_include = 1 //0 for Off and 1 for On Flag

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

Answered my own question here.

I have multiple sites on the same server, so switching it on just a subdomain did nothing. To solve this I had to edit the PHP version in MultiPHP INI Editor.

500 Error: https:// wrapper is disabled in the server configuration by allow_url_fopen=0 when using file_get_contents()

Enabling allow_url_fopen in php.ini according to this answer Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by all did not helped me.

When I searched solution for this question again I found this article Enabling allow_url_fopen

However, since I am using a shared hosting, the support team said to me that this function in php posses security risk, and they do not enable this function in shared server environment and it could only be enabled at my own risk when I am running on vps servers.

So in order to fetch YouTube data via API I had to use cURL function and it worked for me.

http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in

You have to restart your server machine or services in order to reload php.ini file.

Alternative of file_get_contents function

In case you’re using PHP to retrieve data from a certain server you probably came across the problem that it may work for you but a client complained about lots of errors. It’s pretty likely that you’ve relied on the fact that allow_url_fopen is set to true. This way you can put pretty much anything – local path or a URL – into function calls like include or maybe simplexml_load_file.

If you’d like to get around this problem you can advice your client to make the necessary changes in his php.ini file. Most of the time this isn’t an option because the hosting company decided to disable this feature for security reasons. Since almost everybody got cURL installed we can use this to retrieve data from another web server.

Implementation

I’ll present a wrapper that helps you loading an XML file. It uses simplexml_load_file if allow_url_fopen is enabled. If this feature is disabled it employs simplexml_load_string and cURL. If none of this works we’ll throw an exception because we weren’t able to load the data.

class XMLWrapper {

public function loadXML($url) {
if (ini_get('allow_url_fopen') == true) {
return $this->load_fopen($url);
} else if (function_exists('curl_init')) {
return $this->load_curl($url);
} else {
// Enable 'allow_url_fopen' or install cURL.
throw new Exception("Can't load data.");
}
}

private function load_fopen($url) {
return simplexml_load_file($url);
}

private function load_curl($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return simplexml_load_string($result);
}

}

//For Json

class JsonWrapper {

public function loadJSON($url) {
if (ini_get('allow_url_fopen') == true) {
return $this->load_fopen($url);
} else if (function_exists('curl_init')) {
return $this->load_curl($url);
} else {
// Enable 'allow_url_fopen' or install cURL.
throw new Exception("Can't load data.");
}
}

private function load_fopen($url) {
$raw = file_get_contents($url);
$data = json_decode($raw);
return $data;
}

private function load_curl($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$data = json_decode($result);
return $data;
}

}

The code is pretty simple, create an instance of the given class and call the loadXML method. It’ll call the right private method which finally loads the XML. Loading some XML is just an example, you can use this technique with e.g. include or require too.

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

The warning is generated because you are using a full URL for the file that you are including. This is NOT the right way because this way you are going to get some HTML from the webserver. Use:

require_once('../web/a.php');

so that webserver could EXECUTE the script and deliver its output, instead of just serving up the source code (your current case which leads to the warning).



Related Topics



Leave a reply



Submit