0

我有一个 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,我不会收到此错误。

更新2

我试过了:

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

但是一样。

4

0 回答 0