Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use file_get_contents() behind the proxy?

When we use file_get_contents, what does PHP use to resolve the domain name (local DNS??).

I ask because I got this error:

Warning: file_get_contents(http://external-domain.com) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\test.php on line 11

I managed to fix this exact error previously when I used file_get_contents() on an internal URL i.e. http://localhost:9090, the problem was that I didn't have a mapping of localhost to 127.0.0.1 in my hosts file on my windows machine. After adding this, I was able to use file_get_contents() on internal URLs.

Can this be fixed in a similar way?

like image 252
Abs Avatar asked Jan 21 '23 06:01

Abs


2 Answers

Are you behind a proxy? If so, you'll need to tell PHP about it using stream_context_set_default or by passing a context created with stream_context_create as the third param to file_get_contents.

like image 193
bradym Avatar answered Jan 28 '23 23:01

bradym


I had this problem, it was because the value of default_socket_timeout in php.ini was too small.

I would try doubling its value in your case to see if it fixes the issue.

like image 25
Reza Hussain Avatar answered Jan 28 '23 23:01

Reza Hussain