0
$test = json_decode(file_get_contents(URL));
print_r($test);

above code was working perfect just a day back on my bluehost account. not it stopped working and giving me error.

Warning: file_get_contents(URL): failed to open stream: No route to host in /home/xxx/public_html/test1.php on line 8

URL is like external resource of API. any clue about it? i tried to contacted with bluehost support team and they said they can't help me to resolve php errors and my guess is, this is server side issue. edit: same code is working wamp server installed on my local machine. EDIT:

TRACEROUTE Via ssh
traceroute http://url
http://url: Name or service not known
Cannot handle "host" cmdline arg `http://url' on position 1 (argc 1)
code83
  • 21
  • 1
  • 6
  • how is URL constructed? it is something like "`http://example.com`" – Tim G May 02 '15 at 06:52
  • That's puzzling for sure. I wonder if Bluehost has some sort of firewall rule in place that's blocking the route. Do you have SSH enabled on this account? If so, you can log in to your server and do a manual traceroute. It might help troubleshoot your problem. I'd definitely try to escalate this issue with bluehost support. – Tim G May 02 '15 at 06:56
  • @TimG i tried to escalate issue with bluehost team, and they replied they can't help with that and ofc my next question was what is method of refund and closing my account. because i can't afford time delay in it. i need to put it online today on same host or different one. and yes i have SSH access on it. – code83 May 02 '15 at 06:59
  • what is the output from traceroute on the shell? Edit the question and add it. – Tim G May 02 '15 at 07:05
  • Also, if you're still of a mind to talk to BH support, you can safely tell them this is not a PHP error it's a network error - obviously - given the message of `no route to host` – Tim G May 02 '15 at 07:06
  • @TimG in shell i need to just type "traceout" ? – code83 May 02 '15 at 07:06
  • `traceroute hostname` For example, if your URL is http://api.example.com/user/get/123/ you would type in `traceroute api.example.com` – Tim G May 02 '15 at 07:07
  • and it's trace route, not trace out :) – Tim G May 02 '15 at 07:08
  • traceroute just the host, not the url. – Tim G May 04 '15 at 15:36
  • @TimG it working, it was the issue from server side, i had to open the ticket with bluehost and they resolved it. i didn't changed anything in my code or method. and really thanks to you for helping me sorting out the issue. it was you who told me to raise ticket about firewall and no route to host. thanks again :) – code83 May 04 '15 at 21:01
  • This question is marked as duplicate of http://stackoverflow.com/questions/20562368/file-get-contents-how-to-fix-error-failed-to-open-stream-no-such-file, which is not true, because the problems in questions are different (although the error messages are similar). – Rauni Lillemets Sep 06 '16 at 09:15

1 Answers1

0

check configuration on your server using phpinfo().

check "allow_url_fopen" value it should be 1 in php.ini if it is not set 1 then please check with your hosting partner for the same.

You can also call external URL using cURL.

<?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);
}

echo $contents;

?>

Note : check your cURL Extension is enabled on server and in your local system.

Hope this will helps.

Ankur
  • 52
  • 2
  • i did checked "allow_url_fopen" is on, curl is enabled. i tried run above code on server it showing blank page on echo. but same code is running on my local wamp machine. – code83 May 02 '15 at 06:51
  • have you check file permission ? i guess it may happens. – Ankur May 02 '15 at 07:45
  • after chmod 777 to file it gave me 500 Internal Server Error. – code83 May 02 '15 at 07:54
  • can you please try with url_encode. or check in your log file it will gave some information. – Ankur May 02 '15 at 08:17