file_get_contents() fails on one URL, but not others?

I need to make a proxy for my ajax requests. So far it looks like this:


$baseurl = "http://wfs.plansystem.dk/geoserver/wfs?";
//$baseurl = "http://wfs.plansystem.dk/geoserver/welcome.do";
//$baseurl = "http://www.google.com/";
$response = file_get_contents($baseurl);
echo $response;

The odd thing is, I get a PHP error:

[INDENT]Warning: file_get_contents(http://wfs.plansystem.dk/geoserver/wfs?) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C:\xampp\xampp\htdocs\visplaner\proxy\proxy_plansystem_wfs.php on line 12
[/INDENT]
But if I paste the $baseurl value into my browser, I get normal html back from the remote server (All right, it’s an exception - but it’s still a response).

Any other URL I can think of to open (including the geoserver/welcome.do above) with file_get_contents(), my script echos the response. But not with this particular one.

Any idea why?

That server probably shunts the request around, .htaccess and the like etc, either that or it is smart enough to recognise the request is not a real browser. You’d need to use cURL to set correct headers etc.

In the first instance , install LiveHTTPHeaders extension for FF, and replay a request in your browser and see what is going on.

[has a go himself] Hang on, it throws an error page to browsers too, looks like site has problems or that is an incomplete URL etc.


GeoServer - Exception
The following exception was thrown:
java.lang.NullPointerException

you might need to mimic a browsers behavior a bit more closely. For example, you might not be sending certain http headers that the server wants. Some websites load balance by setting a cookie and then redirecting you to the same page(assigning you a server).

try livehttpheaders firefox extension to see what’s going on.

dur…

that url returns a http status of 500. file_get_contents() assumes failure when you get that status.

Thanks for your help. I think I figured it out:
When I request the “bad url” I get an HTTP-header with an error back - AND some html to show in the browser. But the file_get_contents() stops at the HTTP-header and doesn’t read the content.

The odd thing is - the script actually works now (I needed to set an XML header, though):


$baseurl = "http://wfs.plansystem.dk/geoserver/wfs?";
$query = $_SERVER['QUERY_STRING'];
$final_uri = $baseurl.$query;
$response = file_get_contents($final_uri);
header ("Content-Type:text/xml");
echo $response;