简体   繁体   中英

How to change layer style on geoserver using REST API with PHP CURL?

Issue with Geoserver rest api change layer style using PHP curl

I have tried using this code and its not working

curl_setopt($this->ch, CURLOPT_POST, True);
$passwordStr = "admin:geoserer";
curl_setopt($this->ch, CURLOPT_USERPWD, $passwordStr);

curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // -X
curl_setopt($this->ch, CURLOPT_BINARYTRANSFER, false); // --data-binary
curl_setopt($this->ch, CURLOPT_HTTPHEADER, ['Content-Type: text/xml']); // -H

$post = array("<layer><defaultStyle><name>polygon</name></defaultStyle></layer>");
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);

buffer = curl_exec($this->ch);

This is correct CURL request

url -v -u admin:geoserver -XPUT -H "Content-type: text/xml"
-d "<layer><defaultStyle><name>roads_style</name></defaultStyle></layer>"
http://localhost:8080/geoserver/rest/layers/acme:roads

If you're not using 'classic' form data (url encoded or multipart) and setting your own content-type, feed CURLOPT_POSTFIELDS a string instead of an array:

 $post = "<layer><defaultStyle><name>polygon</name></defaultStyle></layer>";
 curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post)

As the manual states:

If value is an array, the Content-Type header will be set to multipart/form-data.

If you run curl request on same server, easiest way is run using exec() php function,

exec('url -v -u admin:geoserver -XPUT -H "Content-type: text/xml"
-d "<layer><defaultStyle><name>roads_style</name></defaultStyle></layer>"
http://localhost:8080/geoserver/rest/layers/acme:roads')

optional. this is function php for change exist layer style in geoserver v2.3.0.

I solved it by folowing function notice that $params it have to put "true" for enable layer after chang style in geoserver.

function change_layer_style($url_layer,$style_name) {
    $params = '<layer><defaultStyle><name>'.$style_name.'</name></defaultStyle><enabled>true</enabled></layer>';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url_layer);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_USERPWD,"user:password"); //geoserver.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Receive server response ...


    $response = curl_exec($ch);

    curl_close ($ch);
    return $response;


}


//--> how to use.
//--> 1. config your geoserver url.
$your_workspace = "xxx";
$your_layer_name = = "bbb";

$url_layer = "http://xxxx.co.uk:8080/geoserver/rest/layers/".$your_workspace.":".$your_layer_name;
$style_name ="your_exist_style_name";

//--> call above function.
change_layer_style($url_layer,$style_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM