0

谷歌显示 411 错误,但我已经将 Content-Length 放在标题中。如何修复此错误?

$authToken = getAuthorizationToken();
$xml_data = '<?XML version="1.0"?>
    <Batch>
   <Remove>    
  <Promotions>      
   <Promotion id="d5111e0a"/>
  </Promotions>
  </Remove>
</Batch>';
$length = strlen($xml_data);
$ch = curl_init("http://www.google.com/cse/api/default/promotions/pe0dnd27zuc");
$header = array();
$header[] = 'Authorization: GoogleLogin auth=' . $authToken;
$header[] = 'Content-Type: text/xml';
$header[] = 'Content-Length: ' . $length;
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
4

2 回答 2

2

从标头数组中删除Content-Length部分。cURL 会自动添加它。所以你不需要发送那个。消除:


//remove following
$header[] = 'Content-Length: ' . $length;

希望能帮助到你

于 2012-02-28T04:17:56.197 回答
0

试试这个:

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch,CURLOPT_POST, $length); //count of your posted array
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
    $result = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

您缺少已发布字段的长度/计数。

于 2012-02-28T08:35:38.960 回答