I am trying to make a simple script in php using curl to work as a proxy for my javascript (OpenLayers) requests to a WFS server on a different domain. It looks like this:
$url=$_REQUEST['url'];
$post='<wfs:GetFeature xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><wfs:Query typeName="feature:Bomen" srsName="EPSG:28992" xmlns:feature="http://bor.grontmij.nl/haarlemmermeer"><ogc:Filter xmlns:ogc="http://www.opengis.net/ogc"><ogc:BBOX><ogc:PropertyName>GEOMETRIE</ogc:PropertyName><gml:Envelope xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:28992"><gml:lowerCorner>107462.72 480068.8</gml:lowerCorner><gml:upperCorner>107644.16 480209.92</gml:upperCorner></gml:Envelope></ogc:BBOX></ogc:Filter></wfs:Query></wfs:GetFeature>';
if (isset($HTTP_RAW_POST_DATA)) {$post=$HTTP_RAW_POST_DATA;}; //else {$post='none';};
$ch = curl_init($url); //.'?request=GetCapabilities'
$postfields=array('filter' => urlencode($post) );
$postfields=array('' => urlencode($post) );
$postfields=array( urlencode($post) );
//file_put_contents ( 'tmp.txt' , $post );
//$postfields=array('file'=>'@tmp.txt');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLINFO_HEADER_OUT, true );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
echo "url: ".$url.'<br/><hr/>';
echo '$HTTP_RAW_POST_DATA: <pre>'.htmlentities($post).'</pre><pre>'.urlencode($post).'</pre><hr/>';
echo '<br/> $content: <pre>'.htmlentities((String)$content).'</pre><hr/>';
foreach($response as $key => $r) {
echo '<pre>$response["'.$key.'"]: '.htmlentities((String)$r).'</pre><hr/>';
};
As you can see above, I have tried out a number of different options.
This is how a WFS standard works and how the OpenLayers WFS protocol class sends the request*. The WFS request is posted - not as key value pairs, but simply as a piece of xml - which I can inspect with Firebug. And it works fine when those requests are sent via OpenLayers (i.e. javascript) directly to a WFS service on the same domain as the rest of my page. But not when sent to my php proxy script and from there to a foreign domain.
It seems as if the CURL functions are only designed for posting key value pairs. If I try to send the xml string as so:
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($post));
I get an exception from the service:
Could not determine geoserver request from http request org.apache.catalina.connector.RequestFacade@6e2752
If I post it as a “normal” array, I get a “Bad request”:
$postfields=array( urlencode($post) );
If I post it as an associative array like one of these options:
$postfields=array(‘filter’ => urlencode($post) );
$postfields=array(‘’ => urlencode($post) );
I get (naturally) this error from the service:
org.xmlpull.v1.XmlPullParserException: only whitespace content allowed before start tag and not - (position: START_DOCUMENT seen -… @1:1)
only whitespace content allowed before start tag and not - (position: START_DOCUMENT seen -… @1:1)
I am sure this is possible somehow. What am I doing wrong …?!?
/jonas