如何使用带有PHP CURL的REST API在geoserver上更改图层样式?

问题描述 投票:0回答:3

使用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
php curl geoserver
3个回答
1
投票

如果您不使用“经典”表单数据(URL编码或多部分),而是设置自己的内容类型,请为CURLOPT_POSTFIELDS输入string而不是数组:

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

如手册所述:

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


1
投票

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

0
投票

可选。

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.
    //$url_layer = "http://xxxx.co.uk:8080/geoserver/rest/layers/your_workspace:your_layer_name";
    //$style_name ="your_exist_style_name";
    //change_layer_style($url_layer,$style_name);
}
© www.soinside.com 2019 - 2024. All rights reserved.