<?php$params1='{ "reportDefinition": { "selector": { "fields": [ "Campaign", "AdGroupId", "Impressions", "Clicks", "Cost" ], "predicates": { "field": "AdGroupStatus", "operator": "IN", "values": [ "ENABLED", "PAUSED" ] } }, "reportName": "Custom Adgroup Performance Report", "reportType": "ADGROUP_PERFORMANCE_REPORT", "dateRangeType": "LAST_7_DAYS", "downloadFormat": "CSV", }}';// Converts the input parameters of the query in JSON format$body = $params1;
//print_r($body);//echo "<br>___________________________________________<br>";
// Creating HTTP request headers
$headers = array( "User-Agent: curl, gzip", "Accept: /", "Accept-Encoding: gzip", "Authorization: Bearer ya29..................g", "developerToken:NB....................Q", "Content-Length: 784", "Expect: 100-continue", "Content-Type: multipart/form-data; boundary=------------------------12d01fae60c7b559",);
echo " < br>Adwords Report<br>";
// Curl initialization$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
/*To fully use the https Protocol, you can enable SSL certificate verification of the direct API server.To enable validation, set the CURLOPT_SSL_VERIFYPEER option to true, and uncomment the string with the CURLOPT_CAINFO option and specify the path to the local copy of the root SSL certificate.*/curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// curl_setopt($curl, CURLOPT_CAINFO, getcwd().'\CA.pem');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_HEADER, true);curl_setopt($curl, CURLINFO_HEADER_OUT, true);curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// - - - Run a loop to execute queries ---// If HTTP code 200 is received, the report content is displayed// If HTTP code 201 or 202 is received, repeated requests are madewhile (true) {
$result = curl_exec($curl);
if (!$result) {
echo ('curl error:'.curl_errno($curl).'- ' .curl_error($curl));
break;
} else {
// Separation of HTTP headers and the response body $responseHeadersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); $responseHeaders = substr($result, 0, $responseHeadersSize); $responseBody = substr($result, $responseHeadersSize);
// Get HTTP status code $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Retrieving HTTP response headers // Request ID $requestId = preg_match('/RequestId: (\d+)/', $responseHeaders, $arr)? $arr[1]: false; // Recommended interval in seconds to check if the report is ready $retryIn = preg_match('/retryIn: (\d+)/', $responseHeaders, $arr)? $arr[1]: 60;
if ($httpCode == 400) {
echo " the query Parameters are incorrect or the limit of reports in the queue has been reached<br>"; echo " RequestId: {$requestId}<br>"; echo " JSON request code:<br>{$body}<br>"; echo " server response JSON:<br>{$responseBody}<br>";
break;
} elseif ($httpCode == 200) {
echo " Report created successfully<br>"; echo " RequestId: {$requestId}<br>"; echo $responseBody;
break;
} elseif ($httpCode == 201) {
echo "the Report was successfully queued in offline mode<br>"; echo " resend request after {$retryIn} seconds<br>"; echo " RequestId: {$requestId}<br>";
sleep($retryIn);
} elseif ($httpCode == 202) {
echo " the Report is generated offline.<br / >"; echo " resend request after {$retryIn} seconds<br>"; echo " RequestId: {$requestId}<br>";
sleep($retryIn);
} elseif ($httpCode == 500) {
echo " an error occurred while generating the report. Please try again at < br>"; echo " RequestId: {$requestId}<br>"; echo " server response JSON:<br>{$responseBody}<br>";
break;
} elseif ($httpCode == 502) {
echo " report generation time exceeded server limit.<br / >"; echo " Please try to change the query parameters-reduce the period and amount of data requested.<br / >"; echo " RequestId: {$requestId}<br>";
break;
} else {
echo " an unexpected error Occurred.<br / >"; echo " RequestId: {$requestId}<br>"; echo " JSON request code:<br>{$body}<br>"; echo " server response JSON:<br>{$responseBody}<br>";
break;
} }}
curl_close($curl);
echo $responseBody;$url = 'https://adwords.google.com/api/adwords/cm/v201809'; at the top in the code
Hello,
Thanks you for reaching the support. The error "The server encountered a temporary error and could not complete your request" could be a transient error. Could you please retry the API request to verify if the issue persists? If the issue is consistent, could you please share the complete request and response logs along with the client customer id for me to investigate this further on my end?
P.S: Please use Reply privately to author option while sharing the details.
Regards,
Nikisha Patel, Google Ads API Team
Hello,
I tried reaching out privately to your personal email. Could you please check your inbox for the email received from my end with the subject: 'Post request for a report' and respond to the same with the requested details for further troubleshooting? Let us know if you are still having trouble sharing the requested information.
Regards,
Nikisha Patel, Google Ads API Team