file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

user1250758 picture user1250758 · Mar 5, 2012 · Viewed 80.9k times · Source

I'm having some weird problems with file_get_contents after moving my site to a new domain. I had to set up a new domain and IP address (using Plesk) to get a new ssl certificate working. Now my file_get_contents calling a script on the same domain is giving me this:

failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

If I call the same url using file_get_contents on another server it works fine, and if I call www.google.com from the server thats failing that works, so it only seems to be if I call a url on the same sever!

I have a feeling it might have something to do with having two IPs with two different ssl certificates on the one server, when i file_get_contents / (index page) of the server from the server I get the plesk 'this is a new domain' page so its like apache isnt looking up the right virtual host when its called from its own sever.

To clarify (hopefully!):

On the server hosting the domain:

file_get_contents('https://mydomain.com?limit=4&offset=0&s_date=2012-02-05&e_date=2012-03-13&order=release_date&dir=desc&cid=12');

gives "failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found"

file_get_contents('http://www.google.com');

works correctly

On another server:

file_get_contents('https://mydomain.com?limit=4&offset=0&s_date=2012-02-05&e_date=2012-03-13&order=release_date&dir=desc&cid=12');

works fine.

I have tried turning ssl off and I still get the same problem.

Answer

GolezTrol picture GolezTrol · Mar 6, 2012

I've had this problem too, when I working on a little test server at home. The domain name is resolved to your external IP address, and a request is sent. But because the request is coming from inside your network, the router doesn't recognise it as a normal request. It probably has a web interface for configuring it, and tries to return a page from its own management system, which is then not found at the path you specified.

In that case, I was working on a Windows PC, and I could solve it by adding the domain I was testing to my hosts file, specifying 127.0.0.1 as the IP-address (or the IP-address of the server, if it is another machine within the same network). In Linux there should be a similar solution, I think.

The problem isn't PHP or your server, but your router.