2

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.

FZs
  • 16,581
  • 13
  • 41
  • 50
  • Well the url: http://www.bitpay.com/ redirects to: https://bitpay.com/. Also the url contents are not in JSON format, but you are using json_decode – Nadir Latif Aug 05 '19 at 11:38
  • Looks like that your server cannot connect to the outside world. If you have permission, try changing the name servers in your `/etc/resolv.conf` file to other nameservers. – Sami Ahmed Siddiqui Aug 05 '19 at 11:41
  • hey, my bad, the url is : $url='https://bitpay.com/api/rates'; – creationist Aug 05 '19 at 11:44
  • " Looks like that your server cannot connect to the outside world. If you have permission, try changing the name servers in your /etc/resolv.conf file to other nameservers " I wish I could but I have no option for this. No resolv.conf file on my server and my Webmin pannel doesn't show anything relative to those configuration files – creationist Aug 05 '19 at 11:45
  • Shall I just create a resolve.conf file from scratch, add the nameserver / domain name I want to resolve and upload it through sftp ? – creationist Aug 05 '19 at 11:46
  • 1
    "I have added the line allow_url_fopen = on in my code" — If your code? That's config setting for php.ini, not something that should be in your code. – Quentin Aug 05 '19 at 11:56
  • I would say that the DNS server probably is not configured correctly. If you're not using a dedicated server then this is probably a question for your hosting company. – apokryfos Aug 05 '19 at 11:56
  • Try the solution in https://stackoverflow.com/questions/542046/php-file-get-contentsloc-fails as well – apokryfos Aug 05 '19 at 11:58
  • When you concluded that you don't have a `/etc/resolve.conf`, how did you go about it? I haven't run into any linux dist that doesn't have that? What OS is the server running on? – M. Eriksson Aug 05 '19 at 11:58
  • Can you ping external website like google.com or any other websites? – Liron Aug 05 '19 at 12:08
  • to Quentin > I know it should be in my php.ini file ( that is inexistant on my remote server by the way ) but for the purpose of the test, I just included it ( the hard way ) on my php page, right above my function. Does it make any difference for this test ? – creationist Aug 05 '19 at 12:08
  • to apokryfos > I guess I am using a shared virtual server. The webmin pannel and the limited functions is probably a clue. – creationist Aug 05 '19 at 12:09
  • to Magnus Eriksson > It's supposedly a linux distro. I just browsed my etc/ folder, there is nothing in it. I just tried to create and upload hosts.conf / resolv.conf with the IP of bitpay.com + the nameserver but still doesn't work – creationist Aug 05 '19 at 12:11
  • to Liron > through my code you mean ? Cuz I repeat, I am personally on a linux distrubution but my server is not hosted locally ( it's hosted remotely on an Apache / Linux distro ) – creationist Aug 05 '19 at 12:11
  • even after manually adding and uploading the file hosts.conf in my etc/ folder, in which I included these following two lines 104.18.90.37 bitpay.com 104.18.90.37 www.bitpay.com well, I still get that warning error message – creationist Aug 05 '19 at 12:14
  • a bit more information about my server / hosting service : Debian GNU/Linux operating system / Dedicated and customized domain / Virtualmin Control Panel with upload/download tools / Isolated server. – creationist Aug 05 '19 at 12:18
  • to apokryfos > I already tried that solution without any positive results – creationist Aug 05 '19 at 12:19
  • Warning: file_get_contents(http://104.18.90.37/): failed to open stream: Network is unreachable in – creationist Aug 05 '19 at 12:22

3 Answers3

0

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.

GAURAV KUMAR JHA
  • 190
  • 1
  • 11
  • Well, I have created a file php.ini that I have uploaded in my public_html folder since no .ini file was existant on my server. Stop me if that was not the right thing to do. Anyway, yes, I have made sure to include the line allow_url_fopen= on but it hasn't solved out my problem. As for the curl exemple / alternative. I have tried it many times before cuz I saw it as a solution on many post but it returns me with a blank page. No result, no error – creationist Aug 05 '19 at 13:26
  • Try to dump the curl resource object and see if there is anything missing. By the way I have used this code and it's working fine. Check it out here: http://versebook.in/stkovflw/test1.php – GAURAV KUMAR JHA Aug 06 '19 at 07:55
  • I can't resolve the host in any cURL function. I tried again and again. I start to suspect that my hosting provider has simply disabled the access to https request on their side. Would it be possible ? – creationist Aug 06 '19 at 08:42
  • BEcause so far, I tried to altternatively switch allow_url_fopen and allow_url_include on and off but didn't change anything. – creationist Aug 06 '19 at 08:43
0

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.

0

Anyone could solve this problem out without having access to the hosts.conf file ?