在php中使用curl更改层的默认样式的问题


Issue with changing the default style of a layer with curl in php

我正在尝试将光栅图层的默认样式从光栅更改为我定义的样式。

我已经创建了一个sld文件,并将其放置在geoserver数据目录的styles文件夹中。

接下来,我使用geoserver文档中给出的以下命令创建了一个XML文件(http://docs.geoserver.org/latest/en/user/rest/examples/curl.html)。

curl -u admin:geoserver -XPOST -H 'Content-type: text/xml' -d '<style><name>aspect_style</name><filename>aspect.sld</filename></style>' http://localhost:8080/geoserver/rest/styles

然后我上传了文件,并使用以下命令将样式应用于图层。

curl -u admin:geoserver -XPUT -H 'Content-type: application/vnd.ogc.sld+xml' -d @aspect.sld http://localhost:8080/geoserver/rest/styles/aspect_raster_style
curl -u admin:geoserver -XPUT -H 'Content-type: text/xml' -d '<layer><defaultStyle><name>aspect_raster_style</name></defaultStyle><enabled>true</enabled></layer>' http://localhost:8080/geoserver/rest/layers/dem:vinnu_aspect_raster

它通过CLI工作,当我在geoserver中看到它时,样式得到了更新。当我对php做同样的操作时,我无法做到这一点。我得到一个错误"输入源不包含任何数据"。

我能够使用curl和php创建工作区、覆盖层、层。但是我无法改变图层的样式。

我的php代码如下。

$file="F:/Vineendra/Images/abcd_aspect_qgis.tif";
$coverage_name="rast";
$workspace="medford";
// Open log file
$logfh = fopen("GeoserverPHP.log", 'w') or die("can't open log file");
// Initiate cURL session
$service = "http://localhost:8080/geoserver/"; 
$request = "rest/layers/".$workspace.":".$coverage_name."_raster"; // to add a new workspace
$url = $service . $request;
$ch = curl_init($url);
// Optional settings for debugging
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //option to return string
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $logfh); // logs curl messages
//Required POST request settings
curl_setopt($ch, CURLOPT_PUT, True);
$passwordStr = "admin:geoserver"; // replace with your username:password
curl_setopt($ch, CURLOPT_USERPWD, $passwordStr);
//POST data
curl_setopt($ch, CURLOPT_HTTPHEADER,
          array("Content-type:text/xml"));
$xmlStr = "<layer><defaultStyle><name>aspect_raster_style</name></defaultStyle></layer>";
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlStr);
//POST return code
$successCode = 201;
$buffer = curl_exec($ch); // Execute the curl request
// Check for errors and process results
$info = curl_getinfo($ch);
if ($info['http_code'] != $successCode) {
  $msgStr = "# Unsuccessful cURL request to ";
  $msgStr .= $url." [". $info['http_code']. "]'n";
  fwrite($logfh, $msgStr);
} else {
  $msgStr = "# Successful cURL request to ".$url."'n";
  fwrite($logfh, $msgStr);
}
fwrite($logfh, $buffer."'n");

curl_close($ch);

由于我已经创建并上传了样式,我直接尝试使用php更改层的默认样式。

不确定这篇文章是否仍然活跃,但如果有人点击此处寻找答案(就像我一样),请确保您在HttpHeader:中通知$xmlString长度


curl_setopt($ch, CURLOPT_HTTPHEADER,
 array(
 "Content-type: application/xml",
 "Content-Length: ".strlen($xmlStr)
 )
);

就我而言,它解决了问题。

不要使用curl_setopt($ch, CURLOPT_PUT, True);

使用curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");