Php 如何使用cURL发出PUT请求?

Php 如何使用cURL发出PUT请求?,php,curl,Php,Curl,我有一个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

我有一个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\u setopt($ch,CURLOPT\u VERBOSE,true)找出哪里出了问题?@TommyBravo:刚才我试着使用
curl\u setopt($ch,CURLOPT\u PUT,1)而不是
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT')。看起来我的数据正在发送到服务器。但我得到了错误:
org.xml.sax.SAXParseException;文件过早结束。
错误是因为
CURLOPT\u PUT
只能与
CURLOPT\u infle
CURLOPT\u inflesize
结合使用。我认为坚持使用
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'PUT')是这里最好的选择。@TommyBravo:那么最后我可以从你的第一条评论中试试它的一句话吗?哦,fogout to说当我使用
CURLOPT_CUSTOMREQUEST
脚本工作30秒后停止。我发现了一个错误。我不使用这个:
curl\u setopt($ch,CURLOPT\u POSTFIELDS,$data)
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);