411 Error Even Though Content-Length is There [closed]

久未见 提交于 2019-12-12 05:01:00

问题


Google displayed a 411 error, but I already put the Content-Length in the header. How do I fix this error?

$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);

回答1:


Remove the Content-Length part from your header array.cURL adds it automatically. So you dont need to send that. Remove:


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

Hope it helps




回答2:


Try this:

    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);

You are missing the length/count of posted fields.



来源:https://stackoverflow.com/questions/9476253/411-error-even-though-content-length-is-there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!