file_get_contents() connection refused for my own site
Have a look at your firewall settings, they may be a little too strict. What happens if you log in and
telnet localhost 80
or the equivalent for your os of choice? And try the same not with localhost but the full ip of your server. Only if it succeeds, you have a curl/php problem.
edit: ok, so connection to localhost
works, using file_get_contents("http://localhost/");
.
This means that you can access you site through localhost, but you need to override the Host:
field sent with the request. This is not exactly normal usage of cURL, but you may try:
curl_setopt(CURLOPT_HTTPHEADER,array('Host: yourdomain.com'));
while requesting URL http://127.0.0.1/
. I wonder if this will be understood by curl but you can give it a shot.
edit^2: If this does not work to trick cURL, just open your own socket connection and make your own request:
$ip = '127.0.0.1';
$fp = fsockopen($ip, 80, $errno, $errstr, 5);
$result = '';
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.exampl.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);
}
(this is an adaption from a php.net example)
Most possibly because your site is pointed to a public ip, which in turn is mapped to an internal IP like mvds pointed out.
www.domain.com = 234.234.234.234
server ip : 10.0.0.1
firewall maps 234.234.234.234 -> 10.0.0.1
from outside networks, but not from internal traffic.
Hence, you need to connect to your machine with the local IP, or localhost (127.0.0.1
) but still maintaining the host-header (www.domain.com
).
Your options are:
Make your provider setup correct routing for their external IPs in their firewall. It's doable but often missed since it's rarely needed. One argument for this to work is because you cannot access other sites that resides on the same network segment as you today.
Tell your provider to add www.domain.com ->
127.0.0.1
in the hosts-file on your serverUse your own socket-code to "fake" the host-header while still connecting to localhost. There are plenty of example classes for this in PHP and mvds gave you an example already.
Find another way to fetch the information. It's on the same server, isn't it? Fetching it by http seems redundant...
The whole problem was to add this line to hosts file ( /etc/hosts )
127.0.0.1 example.com www.example.com
Thanks to jishi! :)