Kamal Saleh
Kamal Saleh

Reputation: 499

file_get_contents( ) not working

I have a script that uses file_get_contents() to get json response from remote server . while file_get_contents() is working properly on local files but not working with http or https it gives me the following error file_get_contents(): http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in and file_get_contents(https://api.domain.com/resolve.json?url=blablabla): failed to open stream: no suitable wrapper could be found .. it is a dedicated server and I have WHM .. I've tried

  1. setting allow_url_fopen = on on WHM PHP configuration Editor but This didn't work.
  2. creating a php.ini file with allow_url_fopen = on in the directory where the error occurred but this didn't work.
  3. added ini_set('allow_url_fopen', 'On'); to the start of the PHP script but this didn't work .

I know I can use Curl instead but I want to know why this is not working .. the script is working properly on other server and localhost

update :

phpinfo();

gives me

allow_url_fopen Off 
allow_url_include   Off 
always_populate_raw_post_data   Off

that means my php.ini changes didn't take effect .. what I'm doing wrong ?

Upvotes: 14

Views: 109681

Answers (7)

Harsh Gupta
Harsh Gupta

Reputation: 1609

In addition to the above answers, it might be possible that SELinux settings might not allow you to connect from httpd. For this you can refer to this link https://drib.tech/programming/php-file_get_contents-not-working

It worked for me

Upvotes: 1

This work perfect:

function get_site($url) { 
  $file = fopen($url, 'r');
  if(!$file) return;
  $buffer='';
  while(!feof($file))   {
    $buffer .= fgets($file, 4096);
    }
  fclose($file);
  return $buffer;
}

Upvotes: 0

Safeer Ahmed
Safeer Ahmed

Reputation: 625

Use curl as an alternative of file_get_contents( ) and this is the function I am using

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

Upvotes: 8

Sahil Jangra
Sahil Jangra

Reputation: 41

$id = $_GET['id'] ;
$api_url = "http://localhost/../API.php?id='$id' ";
$port = 80;
$ch = curl_init( );
curl_setopt ( $ch, CURLOPT_URL, $api_url );
curl_setopt ( $ch, CURLOPT_PORT, $port );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
// Allowing cUrl funtions 20 second to execute
curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 );
// Waiting 20 seconds while trying to connect
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 5 );                                 
$response_string = curl_exec( $ch );

echo $response_string;

This is the best way to solve the web request problems

Upvotes: 1

Dushyant Dagar
Dushyant Dagar

Reputation: 1069

$url= 'https://example.com';

$arrContextOptions=array(
      "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );  

$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

This will allow you to get the content from the url whether it is a HTTPS, no need to change in php ini.

Upvotes: 29

Pham Lai
Pham Lai

Reputation: 151

If you have root access, edit php.ini, usually located at /etc/php.ini.

If you dont have root access, try to add ini_set('allow_url_fopen', 'On'); or ini_set('allow_url_fopen', '1');.

If you can't see php.ini, try using phpinfo() in a PHP script to find the php.ini location.

Upvotes: 2

baao
baao

Reputation: 73241

Login to your server via ssh and type

sudo nano /etc/php5/apache2/php.ini //<<<< ubuntu/debian server, might differ for you

in the file, simply press "ctrl + w" and type "allow_url_fopen" and Return, most probably you will come to the explanation first, so repeat the search a couple of times. Now you can change the entry from

allow_url_fopen=0

to

allow_url_fopen=1

press "ctrl + x" and confirm the file save with "y".

Then type

sudo service apache2 restart

This will restart apache so the new php.ini configuration can be loaded. After those steps, you should be able to use file_get_contents externally.


SIDENOTE

If you can't find your php.ini file, you will find the path to the loaded php.ini file in the top section of your phpinfo()

enter image description here

Upvotes: 10

Related Questions