简体   繁体   English

如何使用带有 PHP CURL 的 REST API 更改地理服务器上的图层样式?

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

Issue with Geoserver rest api change layer style using PHP curl Geoserver rest api 使用 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这是正确的 CURL 请求

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:如果您不使用“经典”表单数据(url 编码或多部分)并设置您自己的内容类型,请向CURLOPT_POSTFIELDS提供字符串而不是数组:

 $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.如果value是一个数组,则 Content-Type 标头将设置为 multipart/form-data。

If you run curl request on same server, easiest way is run using exec() php function,如果您在同一台服务器上运行 curl 请求,最简单的方法是使用 exec() php 函数运行,

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.这是用于更改 geoserver v2.3.0 中的现有图层样式的函数 php。

I solved it by folowing function notice that $params it have to put "true" for enable layer after chang style in geoserver.我通过以下功能解决了这个问题,注意 $params 必须在 geoserver 中的更改样式之后为启用层设置“true”。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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