kari.patila
kari.patila

Reputation: 1079

Reading files from own domain results in 404

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.

Upvotes: 1

Views: 320

Answers (2)

activout.se
activout.se

Reputation: 6116

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.

Upvotes: 2

Bobby Jack
Bobby Jack

Reputation: 16048

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.

Upvotes: 0

Related Questions