简体   繁体   中英

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

Am trying to change the default style of a raster layer from raster to my defined style.

I have created an sld file and placed it in the styles folder of data directory of the geoserver.

Nextly, i have created an XML file using the following command as given in the documentation of the geoserver( 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

And then i have uploaded the file, and applied the style to the layer using the following commands.

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

It worked through CLI, when i see it in geoserver, the style got updated. When i do the same with php, am unable to do that. I get an error as "input source contained no data".

Am able to create the workspace, coveragestore, layer using curl with php. But am unable to change the style of the layer.

My php code is as follows.

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

As i have created and uploaded the style, am directly trying to change the default style of the layer using php.

Not sure if this post is still active but if anyone hits place looking for an answer (like i did), make sure you're informing the $xmlString lenght in the HttpHeader:


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

In my case it solved the problem.

Do not use curl_setopt($ch, CURLOPT_PUT, True);

Using curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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