Search code examples
phpcurlgeoserver

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

Solution

  • 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')