1

I want to change the default style of the layer in Geo server using the REST API.

For this i have this code:

  curl -v -u admin:geoserver -XPUT -H "Content-type: text/xml"
  -d <layer><defaultStyle><name>polygon</name></defaultStyle></layer>"
  http://mindcrewgis.com/geoserver/rest/layers/pymble:pymble_water

when run this command it is working fine form me. But when i convert this command in PHP then it is not working for me My PHP code for above curl command is

<?php

    $url="http://mindcrewgis.com/geoserver/rest/layers/pymble:pymble_water";
    $contentType = 'text/xml';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword"); 

     $data="<layer>
            <defaultStyle>
            <name>polygon</name>
            </defaultStyle>
            </layer>";
     curl_setopt($ch, CURLOPT_PUT, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, 
                array("Content-Type: $contentType",
                'Content-Length: '.strlen($data))
            );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $rslt = curl_exec($ch);

       $info = curl_getinfo($ch);

?> 

But this code is not working for me . There is something wrong in execute curl using PHP.

  • 1
    When you say "is not working", do you get an error back from GeoServer? Have you compared the two queries at the network (e.g. using Wireshark or similar)? Since you are doing HTTP PUT, have you tried using CURLOPT_INFILE and CURLOPT_INFILESIZE instead of CURLOPT_POSTFIELDS (which is documented to be for POST)? – BradHards Apr 28 '14 at 5:34
1

The following is a modified version of what has been working for us. You'll need to supply your own logic for setting the specific style. In our situation, we base the style on the layer name using a series of if/else statements.

import os
from geoserver.catalog import Catalog

"""
geoserver_username - the username of the server
geoserver_password - the password 
geoserver_server - the url for the server (e.g.: http://127.0.0.1:8080).  Note that there is no trailing slash!
"""
def SetGeoServerDefaultStyles(self, geoserver_username, geoserver_password, geoserver_server):

    geoserver_url = geoserver_server + "/geoserver"
    geoserver_catalog_url = geoserver_url + "/gwc/rest"
    cat = Catalog(geoserver_catalog_url, geoserver_username, geoserver_password)

    #Get all layers
    all_layers = cat.get_layers()

    totalLayerCount = len(all_layers)
    print "Total layers: ", totalLayerCount

    for layer in all_layers:

        # Insert your style here.  Originally, we used some logic here to set the style based on the layer name.  Feel free to replace with whatever works best for you.
        style = "<layer><defaultStyle><name>" + styleToUse + "</name></defaultStyle></layer>"

        # Actually make the call to set the default style.  For debugging purposes, you may wish to add
        # the -v (verbose) flag.  
        curlString = 'curl -u ' + geoserver_username + ':' + geoserver_password + ' -XPUT -H "Content-type: text/xml" -d "' + style + '" ' + geoserver_url + '/rest/layers/' + layer.name
        os.system(curlString)

        currentLayer = currentLayer + 1
        print "Processed layer %i of %i" % (currentLayer, totalLayerCount)
0

I solved it by using this way

Change curl_setopt($ch, CURLOPT_PUT, true); to curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.