Search code examples
phprestcurlgeoserver

GeoServer - Add layer with REST API (using PHP)


I'm working on a project using GeoServer (v2.5.2) and GeoWebcache. I want to be able to upload images to create a coverage store and its associated layers. I'm using PHP with cURL to communicate with the REST API.

The upload and the creation of the coverage store works using this code :

$curl = curl_init($service_url."workspaces/".htmlentities($workspace)."/coveragestores");
        $data = '<coverageStore>
                        <name>'.htmlentities($name).'</name>
                        <type>'.htmlentities($type).'</type>
                        <enabled>true</enabled>
                        <connectionParameters>
                          <entry key="url">file:'.$file.'</entry>
                          <entry key="namespace">'.htmlentities($workspace, ENT_COMPAT).'</entry>
                        </connectionParameters>
                  </coverageStore>';
        curl_setopt($curl, CURLOPT_POST, True);
        curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-type: application/xml, Content-Length: ".strlen($data)));
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_USERPWD, $auth);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $buffer = curl_exec($curl);

Now I want to create a new layer. I'm using the same code as above (only changing URL and XML parameters).

The best "documentation" I've found is this one : http://docs.geoserver.org/2.5.x/en/user/geowebcache/rest/layers.html Tried to use the GeoWebCache and GeoServer version, both return me a 404 error code.

So I've tried again using this doc : http://docs.geoserver.org/2.5.x/en/user/rest/api/layers.html But it don't say what are the POST parameters expected. The only thing I get is a 500 error code.

What am I doing wrong ? Thanks a lot.


Solution

  • Found my mistake : I was trying to add a layer but I needed to add a coverage, the layer is automatically created with it.

    If it can help someone, this is how I've done :

            $data = '<coverage>
                        <name>'.htmlentities($name).'</name>
                        <title>'.htmlentities($name).'</title>
                        <nativeCRS>'.htmlentities('
                            GEOGCS["WGS 84", 
                            DATUM["World Geodetic System 1984", 
                              SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], 
                              AUTHORITY["EPSG","6326"]],
                            PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], 
                            UNIT["degree", 0.017453292519943295], 
                            AXIS["Geodetic longitude", EAST], 
                            AXIS["Geodetic latitude", NORTH], 
                            AUTHORITY["EPSG","4326"]]
                            ').'
                        </nativeCRS>
                        <supportedFormats>
                             <string>GEOTIFF</string>
                             <string>PNG</string>
                             <string>JPEG</string>
                             <string>TIFF</string>
                        </supportedFormats>
                        <requestSRS>
                            <string>EPSG:4326</string>
                        </requestSRS>
                        <responseSRS>
                            <string>EPSG:4326</string>
                        </responseSRS>
                        <srs>EPSG:4326</srs>
                    </coverage>';
    
    $curl = curl_init($service_url."workspaces/".htmlentities($workspace)."/coveragestores/".htmlentities($name)."/coverages");
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-Type: application/xml","Content-Length: ".strlen($data)));
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_USERPWD, $auth);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_exec($curl);