title: Google Custom Search API adding annotations programmatically through php via cURL
body: <p>I'm trying to add annotations (sites to search) to my google custom search engine programmatically by POSTing xml using php and cURL.  According to their developer documentation, all I need to do is:</p>
  
  <pre><code>POST http://www.google.com/cse/api/default/annotations/
  Content-Type: text/xml
  Authorization: GoogleLogin auth="IM6F7Cx2fo0TAiwlhNVdSE8Ov8hw6aHV"
  
  &lt;Batch&gt;
  &lt;Add&gt;
  &lt;Annotations&gt;
    &lt;Annotation about="http://www.solarenergy.org/*"&gt;
      &lt;Label name="_cse_solar_example"/&gt;
    &lt;/Annotation&gt;
    &lt;Annotation about="http://www.solarfacts.net/*"&gt;
      &lt;Label name="_cse_solar_example"/&gt;
    &lt;/Annotation&gt;
    &lt;Annotation about="http://en.wikipedia.org/wiki/*"&gt;
      &lt;Label name="_cse_exclude_solar_example"/&gt;
    &lt;/Annotation&gt;
  
    &lt;/Annotations&gt;
    &lt;/Add&gt;
    &lt;/Batch&gt;
  </code></pre>
  
  <p>However, when I use the following code, I get an error 411, "POST requests require a Content-length header"</p>
  
  <pre><code>function getAuthorizationToken(){
  $ch = curl_init();
  $url = 'https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&amp;Email=***&amp;Passwd=***&amp;service=cprose&amp;source=***';
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_HEADER, FALSE); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
  $authTokenData = curl_exec($ch);
  $authTokenArray = explode('Auth=', $authTokenData);
  $authToken = $authTokenArray[1];
  curl_close($ch);
  return $authToken;
  }
  
  $authToken = getAuthorizationToken();
  
  $url = 'http://www.google.com/cse/api/default/annotations/';
  $xml_data = '&lt;?XML version="1.0"?&gt;
  &lt;Batch&gt;
    &lt;Add&gt;
      &lt;Annotations&gt;
        &lt;Annotation about="http://www.solarenergy.org/*"&gt;
          &lt;Label name="_cse_solar_example"/&gt;
        &lt;/Annotation&gt;
        &lt;Annotation about="http://www.solarfacts.net/*"&gt;
          &lt;Label name="_cse_solar_example"/&gt;
        &lt;/Annotation&gt;
        &lt;Annotation about="http://en.wikipedia.org/wiki/*"&gt;
          &lt;Label name="_cse_exclude_solar_example"/&gt;
        &lt;/Annotation&gt;
       &lt;/Annotations&gt;
    &lt;/Add&gt;
  &lt;/Batch&gt;';
  $length = strlen($xml_data);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Authorization: GoogleLogin auth=".        $authToken, "Content-Type: text/xml", "Content-Length: " . $length));
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // ask for results to be returned
  $result = curl_exec($ch);
  curl_close($ch);
  
  echo $result;
  </code></pre>
  
  <p>I've been struggling with this issue for a very long time, and I would greatly appreciate any help with this.</p>
  
  <p>Thank you</p>