我有一个PUT请求:
curl.exe -v -u admin:geoserver -XPUT -H "Content-type: application/vnd.ogc.sld+xml" -d @tester.sld http://localhost:8080/geoserver/rest/styles/tester
现在我想在php中生成相同的请求。我这样做:
$contentType = 'application/vnd.ogc.sld+xml';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/geoserver/rest/styles/tester');
curl_setopt($ch, CURLOPT_USERPWD, "admin:geoserver");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("Content-Type: $contentType",
'Content-Length: '.strlen(stripslashes($data)))
);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rslt = curl_exec($ch);
$info = curl_getinfo($ch);
样本数据:
<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
xmlns="http://www.opengis.net/sld"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NamedLayer>
<Name>Dashed line</Name>
<UserStyle>
<Title>SLD Cook Book: Dashed line</Title>
<FeatureTypeStyle>
<Rule>
<LineSymbolizer>
<Stroke>
<CssParameter name="stroke">#0000FF</CssParameter>
<CssParameter name="stroke-width">3</CssParameter>
<CssParameter name="stroke-dasharray">5 2</CssParameter>
</Stroke>
</LineSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
我没有收到任何错误,但上面的curl请求将我的数据放在服务器上,但这个php代码不一样。
代码中有错误吗?
更新
刚才我尝试使用curl_setopt($ch, CURLOPT_PUT, 1);
代替curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
。看起来我的数据发送到服务器。但我收到错误:org.xml.sax.SAXParseException; Premature end of file.
如果我在控制台中使用cURL,我不会收到此错误。
UPDATE2
我受到了审判:
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
但同样。