How to use file_get_contents () behind a proxy server?

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: connection attempt failed because the connected party did not respond correctly after a period of time or the connection was established because the connected host could not respond. in C: \ test.php on line 11

I managed to fix this exact error earlier when I used file_get_contents()the internal URL, i.e. http://localhost:9090The problem was that I did not have a localhost mapping to 127.0.0.1 in my hosts file in my windows machine. By adding this, I was able to use file_get_contents()for internal urls.

Can this be eliminated in a similar way?

+3
source share
4 answers

Are you for the proxy? If so, you need to report it to PHP. stream_context_set_default or by passing the context created using stream_context_create as the third parameter to file_get_contents .

+3
source

, default_socket_timeout php.ini .

, , .

+2

I had a similar problem (but the file is on the same server), which I had many hours to find out. Then it did the trick:

$fileUrl = "http://selfDomain.com/file.txt";

//external path to local path fix
$fileUrl = str_replace($_SERVER["SERVER_NAME"], $_SERVER["LOCAL_ADDR"], $fileUrl);
+1
source

Just use it, your problem is resolved.

file_get_contents("http://external-domain.com",true) 
0
source

Source: https://habr.com/ru/post/1769514/


All Articles