maric Posted August 12, 2008 Share Posted August 12, 2008 I'm having a problem returning an image that is dynamically generated in response to a http-posted XML. I either get a screen full of gibberish, or a message of "The image [http://PHPGetMap.php?blahblahblah] cannot be displayed, because it contains errors." The eventual goal is as follows. On a web page, user selects a location, which in turn sets the src of an img tag to be "PHPGetMap.php?lat=42.111&lon=-71.232". PHPGetMap.php inserts the uservariables (lat and lon), into the XML, and then posts the URL to the web map service. The web map service creates and returns an image (format is hardcoded to either PNG or JPEG) to the PHP, which then needs to return it to the original web page in response to the img tag having had the src changed. Because of cross site scripting limitations, this http-post cannot come directly from javascript, and thus I started trying to learn PHP to post the xml and return the response. This said, I'm a bit new, and also am restricted (for the time being) to PHP4.3.2. Below I've done a static version of the example (ie: just a PHP that does the http-post, recieves the image response, and returns that image). First, a brief bit of explanation on the web map service. The web map service in question is MassGIS' Geoserver, (http://lyceum.massgis.state.ma.us/wiki/doku.php?id=wms:home) HTTP-Posting the following XML to http://giswebservices.massgis.state.ma.us/geoserver/wms will return a PNG file. The following XML (called an SLD) is a simplified version of the first example @ http://lyceum.massgis.state.ma.us/wiki/doku.php?id=wms:sld:point_example and displays all airports near boston. <?xml version="1.0" encoding="UTF-8"?> <wms:GetMap service="WMS" version="1.3.0" xmlns:gml="http://www.opengis.net/gml" xmlns:ows="http://www.opengis.net/ows" xmlns:sld="http://www.opengis.net/sld" xmlns:wms="http://www.opengis.net/ows" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/ows http://giswebservices.massgis.state.ma.us/geoserver/schemas/sld/GetMap.xsd"> <sld:StyledLayerDescriptor version="1.0.0"> <sld:NamedLayer> <sld:Name>massgis:GISDATA.OUTLINE25K_ARC</sld:Name> <sld:NamedStyle> <sld:Name>GISDATA.OUTLINE25K_ARC::Default</sld:Name> </sld:NamedStyle> </sld:NamedLayer> <sld:NamedLayer> <sld:Name>massgis:GISDATA.AIRPORTS_PT</sld:Name> <sld:NamedStyle> <sld:Name>GISDATA.AIRPORTS_PT::Default</sld:Name> </sld:NamedStyle> </sld:NamedLayer> </sld:StyledLayerDescriptor> <wms:BoundingBox srsName="EPSG#26986"> <gml:coord> <gml:X>209899</gml:X> <gml:Y>874160</gml:Y> </gml:coord> <gml:coord> <gml:X>266889</gml:X> <gml:Y>932123</gml:Y> </gml:coord> </wms:BoundingBox> <wms:Output> <wms:Format>image/png</wms:Format> <wms:Transparent>false</wms:Transparent> <wms:BGcolor>0xFFFFFF</wms:BGcolor> <wms:Size> <wms:Width>234</wms:Width> <wms:Height>239</wms:Height> </wms:Size> </wms:Output> <wms:Exceptions>application/vnd.ogc.se+inimage</wms:Exceptions> </wms:GetMap> I can test this post by building the post request in Fiddler, and I recieve my PNG response. Response size is 10,632 bytes. Below is my attempt at a PHP to post the xml, get the response and return the response. If I include the line: header("Content-Type: image/png"); then calling the php gives a response size of 10,962 bytes and gives the message "The image "http://myURL/PHPGetMap.php" cannot be displayed, because it contains errors." Adding the other headers (Content-Transfer-Encoding: binary, Content-length: ".strlen($image)) backin results in no change to the error message. If I don't include the header line, I get the same response size and the following output: "HTTP/1.0 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=3DEDAB6FAB6FB4269E6516C1EEFAE31A; Path=/geoserver Content-Type: image/png Date: Tue, 12 Aug 2008 16:25:16 GMT X-Cache: MISS from giswebservices.massgis.state.ma.us Via: 1.0 webservices-1.massgis.state.ma.us (squid/3.0.PRE5) Connection: close ?PNG ??? IHDR???????????s;???)OI..." and the gibberish continues for awhile Any ideas? PHPGetMap.php <?php // Step 1, Define SLD XML to Post $xml_req ='<?xml version="1.0" encoding="UTF-8"?> <wms:GetMap service="WMS" version="1.3.0" xmlns:gml="http://www.opengis.net/gml" xmlns:ows="http://www.opengis.net/ows" xmlns:sld="http://www.opengis.net/sld" xmlns:wms="http://www.opengis.net/ows" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/ows http://giswebservices.massgis.state.ma.us/geoserver/schemas/sld/GetMap.xsd"> <sld:StyledLayerDescriptor version="1.0.0"> <sld:NamedLayer> <sld:Name>massgis:GISDATA.OUTLINE25K_ARC</sld:Name> <sld:NamedStyle> <sld:Name>GISDATA.OUTLINE25K_ARC::Default</sld:Name> </sld:NamedStyle> </sld:NamedLayer> <sld:NamedLayer> <sld:Name>massgis:GISDATA.AIRPORTS_PT</sld:Name> <sld:NamedStyle> <sld:Name>GISDATA.AIRPORTS_PT::Default</sld:Name> </sld:NamedStyle> </sld:NamedLayer> </sld:StyledLayerDescriptor> <wms:BoundingBox srsName="EPSG#26986"> <gml:coord> <gml:X>209899</gml:X> <gml:Y>874160</gml:Y> </gml:coord> <gml:coord> <gml:X>266889</gml:X> <gml:Y>932123</gml:Y> </gml:coord> </wms:BoundingBox> <wms:Output> <wms:Format>image/png</wms:Format> <wms:Transparent>false</wms:Transparent> <wms:BGcolor>0xFFFFFF</wms:BGcolor> <wms:Size> <wms:Width>234</wms:Width> <wms:Height>239</wms:Height> </wms:Size> </wms:Output> <wms:Exceptions>application/vnd.ogc.se+inimage</wms:Exceptions> </wms:GetMap>'; // Step 2, Set URL to post to $url = "http://giswebservices.massgis.state.ma.us/geoserver/wms"; // Step 2.5, Post function function do_post_request($url, $xml_req, $optional_headers = null) { //echo ("in do_post_request"); $start = strpos($url,'//')+2; $end = strpos($url,'/',$start); $host = substr($url, $start, $end-$start); $domain = substr($url,$end); $fp = pfsockopen($host, 80); if(!$fp) return null; fputs ($fp,"POST $domain HTTP/1.1\n"); fputs ($fp,"Host: $host\n"); if ($optional_headers) { fputs($fp, $optional_headers); } fputs ($fp,"Content-type: text/xml\n"); fputs ($fp,"Content-length: ".strlen($xml_req)."\n\n"); fputs ($fp,"$xml_req\n\n"); $response = ""; while(!feof($fp)) { $response .= fgets($fp, 1024); } fclose ($fp); return $response; } $image = do_post_request($url, $xml_req); header("Content-Type: image/png"); //header("Content-Transfer-Encoding: binary"); //header("Content-length: ".strlen($image)); echo $image; ?> Link to comment https://forums.phpfreaks.com/topic/119321-problem-displaying-image-recieved-by-http-post-of-xml-to-web-map-service-geoser/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.