411即使内容长度存在,也会出错

时间:2012-02-28 04:11:10

标签: php content-length

Google显示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);

2 个答案:

答案 0 :(得分:2)

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


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

希望有所帮助

答案 1 :(得分: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);

您缺少已过帐字段的长度/数量。