content-length is required in php

475 views
Skip to first unread message

mk...@northcoastmed.com

unread,
Oct 24, 2013, 5:44:01 PM10/24/13
to adwor...@googlegroups.com
Hello,

I'm trying to send a simple post request to the api to download a report.

And getting a 411 error message which says "POST requests require a content-length header. That's all we know."

I'm using the example in here.

Also tried the new api url

Has anybody ever come across with this?
Any ideas?

Thanks!

<?php
// Provide header information.
$authToken
= 'INSERT_AUTH_TOKEN_HERE';
$clientCustomerId
= 'INSERT_CLIENT_CUSTOMER_ID_HERE';
$developerToken
= 'INSERT_DEVELOPER_TOKEN_HERE';

// Create report definition XML.
$reportDefinition
= <<<EOT
<reportDefinition xmlns="https://adwords.google.com/api/adwords/cm/v201109">
 
<selector>
   
<fields>CampaignId</fields>
    <fields>Id</
fields>
   
<fields>Impressions</fields>
    <fields>Clicks</
fields>
   
<fields>Cost</fields>
    <predicates>
      <field>Status</
field>
     
<operator>IN</operator>
      <values>ENABLED</
values>
     
<values>PAUSED</values>
    </
predicates>
 
</selector>
  <reportName>Custom Adgroup Performance Report</
reportName>
 
<reportType>ADGROUP_PERFORMANCE_REPORT</reportType>
  <dateRangeType>LAST_7_DAYS</
dateRangeType>
 
<downloadFormat>CSV</downloadFormat>
</
reportDefinition>
EOT
;

// Create parameters.
$params
= array('__rdxml' => $reportDefinition);

// Create headers.
$headers
= array();
$headers
[]= 'Authorization: GoogleLogin auth=' . $authToken;
$headers
[]= 'clientCustomerId: ' . $clientCustomerId;
$headers
[]= 'developerToken: ' . $developerToken;
$headers[]= 'content_length: ' . trim(strlen(print_r($params, true)));
$headers[]= 'Content-Type: application/x-www-form-urlencoded';

$downloadPath
= 'report.csv';
$url
= 'https://adwords.google.com/api/adwords/reportdownload/v201109';

$file
= fopen($downloadPath, 'w');
$ch
= curl_init($url);
curl_setopt
($ch, CURLOPT_FILE, $file);
curl_setopt
($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt
($ch, CURLOPT_HTTPHEADER, $headers);
curl_exec
($ch);
$code
= curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close
($ch);
fclose
($file);

if ($code == 200) {
  printf
("Report downloaded to: %s\n", $downloadPath);
} else {
  $file
= fopen($downloadPath, 'r');
  $error
= fread($file, 1024);
  fclose
($file);
  printf
("Error: %s\n", $error);
}

Paul Matthews (AdWords API Team)

unread,
Oct 31, 2013, 2:38:16 PM10/31/13
to adwor...@googlegroups.com
Hi There,

It seems that you've added the "content_length" and "Content-Type" parameters. Firstly, the content length parameter is defined as "Content-Length" and I believe you'd want to trim before you use strlen.

Then I notice you specify the Content-Type, but with an array of items you actually want "Content-Type: multipart/form-data" which is set for you if you leave it blank.

Regards,

- Paul, AdWords API Team.

Mike Young

unread,
Feb 19, 2014, 6:25:30 AM2/19/14
to adwor...@googlegroups.com

Hi Paul,

I have the same problem with  my script DownloadcriteriaReportWithAWQL.php in the V201309 library

I have stringed two reports together one downloading a report for yesterday (yesterday.xml) and another that downloads todays report (today.xml)
Then it will unpick the XML after.
All fine when I use one clientCustomerId in the auth.ini file.

I was using the older client library (201306) with getcampaignstats.php and each time it was run it set a new clientCustomerId in the adwords user.php file using
$this->SetClientCustomerId($newclientid);
which worked and changed the number each time, via a text file reading a new client ID with a counter.

But now I have updated the library I am getting this error, I have not added anything with headers etc. (not even thought about it!)
It now does not download the XML files but an error file that says this

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 411 (Length Required)!!1</title>
  STYLE STUFF!
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>411.</b> <ins>That’s an error.</ins>
  <p>POST requests require a <code>Content-length</code> header.  <ins>That’s all we know.</ins>

It does not seem to like a different number each time. When it is one number in the auth.ini file it works.

How do I stop this?


Ray Tsang (AdWords API Team)

unread,
Mar 4, 2014, 1:58:26 PM3/4/14
to adwor...@googlegroups.com
Mike,

Could I trouble you to clarify the issue?  Are you having trouble with DownloadcriteriaReportWithAWQL.php as is?  Or are you having trouble when you are not using a client library?

Thanks!

Ray

Ray Tsang (AdWords API Team)

unread,
Mar 5, 2014, 1:58:31 PM3/5/14
to adwor...@googlegroups.com
Mike,

It seems like there were issues in the data that's being posted to retrieve the report.  Please see our Reporting Basics guide.

For example:
  • The XML needs to be encoded into the __rdxml parameter.
  • Authorization header should read Authorization: Bearer ....access_token...

That being said, I feel you'll have an easier time if you used one of our client libraries to pull the reports.  Every client library has an example on reporting.  I noticed you tried using v201306.  Please note that the reports are available in v201309.

Cheers,

Ray
Reply all
Reply to author
Forward
0 new messages