[GIS] Send XML WFS Request via PHP

geoserverPHPwfs

I'm trying to build a web mapping app that retrieves data from our WFS server. I'd like to avoid JavaScript for the main search scripts.

From what's I've read the best way to send the XML POST request to the server is to use CURL.

I have tested the following very simple XML in GeoServer and it's fine. Hwever, when I send the request as below I get an error.

I have trawled Google but can't seem to find any examples of doing this. I can't believe I'm the only one trying to do this!

The PHP Code:

<?php 
//Set variables.
$url = "http://squirrel.bis.local:8080/geoserver/wfs";
$post = '<GetCapabilities 
    service="WFS" 
    xmlns="http://www.opengis.net/wfs" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0    /wfs.xsd"/>';

$post = str_replace("\r", "", $post); //Clean string.
$post = str_replace("\n", "", $post);
$post = str_replace("\t", "", $post);
$post = str_replace("> <", "><", $post); //Clean any leading spaces between elements.

//What are we sending?
echo "Post Request:<br />";
echo htmlentities($post);
echo "<br /><br />";

//Send post request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$content=curl_exec($ch);

//Collect result.
$result = curl_exec($ch);
curl_close ($ch);

//What did we get?
echo "Post Response:<br />";
echo "<pre>" . htmlentities($result) . "</pre>";
echo "<br /><br />";

?>

The error is:

<ows:ExceptionReport version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/ows 
http://squirrel.bis.local:8080/geoserver/schemas/ows/1.0.0                   /owsExceptionReport.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:ows="http://www.opengis.net/ows">
<ows:Exception exceptionCode="MissingParameterValue" locator="request">
<ows:ExceptionText>Could not determine geoserver request from http request         org.geoserver.platform.AdvancedDispatchFilter$AdvancedDispatchHttpRequest@19f0b0a6
</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>

Thanks
Steve

Best Answer

You have to make sure to set the HTTP Content-Type/Content-Length headers:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml', 'Content-Length: '.strlen($post)));

In this case, the value of Content-Length would be the size of your XML request.

Related Question