0

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);
Community
  • 1
  • 1
propostaff
  • 55
  • 3
  • 10
  • Any Ideas? When i am delete $header[] = 'Content-Length: ' . $length; and curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); - code work good and send me all promotions ID at xml – propostaff Feb 28 '12 at 04:27

2 Answers2

2

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

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • HTTP 1.1 error 411 refers to Length Required: The server refuses to accept the request without a defined Content-Length. But I thought this is added by curl action itself. – Raptor Feb 28 '12 at 04:20
  • It did not help. Still the same 411 error.. – propostaff Feb 28 '12 at 04:20
0

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.

Maz
  • 101
  • 7