SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    PHP Code:
    $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:
    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
    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?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.
    Code:
    GeoServer - Exception
    The following exception was thrown:
    java.lang.NullPointerException

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dur...

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

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2007
    Location
    Bilthoven, Netherlands
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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):
    PHP Code:
    $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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •