skip to Main Content

I am trying to run a small website that is hosted on a distant virtual server.

My code was working fine when I was running my tests locally, on my machine through Apache 2.0.

Now that it’s hosted remotely ( Apache 2 + Webmin + latest version of PHP ), seems like I’m having some issue with the famous function file_get_contents. Looks like my server doesn’t allow the resolution of any external domain.

I have read multiple topics and tried many solutions but seems like none of them has worked so far.

Here is the function I have a problem with

$url='https://bitpay.com/api/rates';
$json=json_decode( file_get_contents( $url ) );
$dollar=$btc=0;

foreach( $json as $obj ){
    if( $obj->code=='USD' )$btc=$obj->rate;
}

echo "1 bitcoin=$" . $btc . "USD<br />";
$dollar=1 / $btc;
echo "10 dollars = " . round( $dollar * 10,8 )."BTC";

exit();

returns me with this error whatever I try

Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in

I tried to locate the file etc/host on my server but it’s inexistent.

I have no idea how to setup/parameter any hostname.

I don’t have a lot of options in my Webmin panel so I’m wondering what I can do to work toward the resolution of this problem.

I have added the line allow_url_fopen = on in my code but it doesn’t work either.

Finally, I tried the curl alternative that so many of you are advising.

But when I try to echo or print_r the output/result, I get a blank page. Not even a single error message.

3

Answers


  1. Chosen as BEST ANSWER

    I made a few changes to my php.ini file.

    here is the content of my current php.ini file

    allow_url_fopen = On;
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    

    But I realised something intriguing : regarding this line

    ini_set('display_errors', 1);

    If I don't add it in my php file where my code is, then the error are not displayed. Seems like my ini file is not a the right place or that taken into consideration by the server.

    I'm really getting confused because it's very different from the local hosting

    I feel my server configuration is null.

    None of the config file can be found in the etc directory and I my php.ini file is " not working " obivously.


  2. file_get_contents don’t allow fetching external url until allow_url_fopen is set to On.
    Since you have already set it to On. you can try this code:

    <?php
    
    function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
    }
    
    echo get_content('https://bitpay.com/');
    
    ?>
    

    However the page is not going to load properly because on the target site the absolute path for the resources(images, scripts, etc) are used. So need to the path to full URL.

    Login or Signup to reply.
  3. Anyone could solve this problem out without having access to the hosts.conf file ?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search