tags:

views:

105

answers:

2

When I try to use curl or file_get_contents to read something like http://example.com/python/json/ from http://example.com/ I should be getting a JSON response, but instead I get a 404 error. Using curl or any other method outside my own domain works perfectly well.

echo file_get_contents('http://example.com/python/json/'); => 404
echo file_get_contents('http://google.com'); => OK

The same script works on my laptop, but I can't figure out what the difference is.

+1  A: 

It looks like example.com is not the default domain for the IP address and that file_get_contents uses HTTP/1.0 instead of HTTP/1.1 and/or does not send a Host: header. Try the curl support in PHP instead:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://example.com/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

Alternatively the /etc/hosts file sets the wrong IP address for example.com.

divideandconquer.se
Thank you. I changed the domain name to IP address and we're all set.
kari.patila
A: 

Something to do with the domain, maybe? Of course, you don't want to reveal it here, so you might have a bit to do, but I guess the sanity check would be that you can access that URL via a web browser (or command line tool such as GET if you're command-line only).

You could also just ping and/or traceroute to the domain - the results may well be illuminating.

Bobby Jack