0

I was using file_get_contents() to fetch information from an external URL. It was working perfectly on the server before. But now, somehow it fails to work on the server. (No changes in the codes.) It keeps giving me the error: failed to open stream: Connection timed out.

I have tested it on localhost and it works perfectly. I have checked allow_url_fopen option, it is still On.

So, what could be the reason(s)?

d.yuk
  • 809
  • 1
  • 12
  • 30
  • 2
    May be the source server blocked your host? – SajithNair Mar 17 '14 at 04:35
  • @SajithNair Thanks for your reply. Oh well... so is there any way to check if that really happens? – d.yuk Mar 17 '14 at 04:39
  • Do you have ssh access to your server? – SajithNair Mar 17 '14 at 04:43
  • 1
    Another reason could be, your timeout setting in `php.ini`. Check the value of `default_socket_timeout` in your `php.ini`. You can also override it by giving `ini_set('default_socket_timeout', 900);` in your php code (in case your timeout is low and you are trying to access a big file) – SajithNair Mar 17 '14 at 04:50
  • Well, I am afraid I do not have ssh access. And I have just tried to set the timeout setting, but it didnt work as well, no matter I use `file_get_contents` or `cURL`. And I just tried to access other sites like `Google` and that worked properly. So I guess it is really quite likely that the target site has blocked the host... – d.yuk Mar 17 '14 at 05:02

1 Answers1

0

file_get_contents does not work well at all with getting remote files and should not be used. It does not deal with slow network connections or redirects, and does not return error codes. You should use curl instead to fetch remote files.

There is an example in the manual for curl_exec: http://us3.php.net/manual/en/function.curl-exec.php

krish
  • 221
  • 1
  • 4
  • 20
  • Can you give any proof for this? –  Mar 17 '14 at 04:51
  • 1
    IN another Case,To use file_get_content to read a web address allow_url_fopen has to be set to on. Have you checked that? In Another case If you do not have access to the php.ini on a hosted service you can turn it on from the .htaccess file using php_value allow_url_fopen On – krish Mar 17 '14 at 04:54
  • Sorry but I already mentioned that I have `allow_url_fopen` set to `On`. – d.yuk Mar 17 '14 at 04:59
  • these are some quastion already check in stackoverflow http://stackoverflow.com/questions/18447827/file-get-contents-connection-timed-out-for-some-contents – krish Mar 17 '14 at 05:07
  • http://stackoverflow.com/questions/14455403/failed-to-open-stream-connection-timed-out-error-themoviedb – krish Mar 17 '14 at 05:07
  • @krish thanks for your help. I have tried to use cURL, but it didnt work as well. – d.yuk Mar 17 '14 at 05:21
  • ok i will search this things and i got the answer then i will tell u – krish Mar 17 '14 at 05:33