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

时间:2014-08-17 11:57:23

标签: php curl geoserver

使用PHP curl

的Geoserver rest api更改图层样式的问题

我尝试使用此代码并且无效

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

这是正确的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

3 个答案:

答案 0 :(得分:1)

如果您不使用经典&#39;表单数据(网址编码或多部分)并设置您自己的内容类型,提供CURLOPT_POSTFIELDS 字符串而不是数组:

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

正如手册所述:

  

如果value是数组,则Content-Type标头将设置为multipart / form-data。

答案 1 :(得分:0)

如果在同一台服务器上运行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')

答案 2 :(得分:0)

可选。这是用于更改Geoserver v2.3.0中存在图层样式的功能php。

我通过关注功能通知来解决它,即$ params必须在geoserver中的chang样式之后为启用层放置“ 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);