<?
// Copyright 2005, Google Inc. All rights reserved.
require_once('nusoap.php');
/*
* This sample program illustrates how to schedule and download a
keyword report
* This program uses nusoap.php to handle the SOAP interactions.
* To try it out, you'll need to copy it to your local directory
because it writes out a report.
*/
// Set up the authentication headers
$email = "<email>x...@gmail.com</email>";
$password = "<password>xyz</password>";
$userAgent = "<useragent>zz</useragent>";// what is this?
$token = "<token>xyz</token>";
$header = $email . $password . $userAgent . $token;
// Connect to the WSDL for the ReportService
$wsdl = "https://adwords.google.com/api/adwords/v2/ReportService?wsdl";
$client = new soapclient($wsdl, 'wsdl');
// Set the headers; they are needed for authentication
$client->setHeaders($header);
// Specify the report criteria
$reportName = "<name>Keyword Report Test</name>"; //what variable used
here?
$aggregation = "<aggregationType>Summary</aggregationType>";
$startDate = "<startDate>2006-01-01T00:00:00Z</startDate>";
$endDate = "<endDate>2006-03-30T00:00:00Z</endDate>";
// Could optionally specify campaigns, but the default is to get all
campaigns
// <campaigns>1234567</campaigns>
$reportjobparams = "<job xsi:type='KeywordReportJob'>" . $reportName .
$aggregation . $startDate . $endDate . "</job>";
// Construct the XML string for the parameters
// It's a nusoap thing that the param string needs to include the
operation name too
$reportJob = "<scheduleReportJob
xmlns='https://adwords.google.com/api/adwords/v2'>" . $reportjobparams
. "</scheduleReportJob>";
// Make the request to schedule the report job
$jobid = $client->call("scheduleReportJob", $reportJob);
$jobid = $jobid['scheduleReportJobReturn'];
// Handle any SOAP faults.
if($client->fault) {
echo "<P>FAULT: {$client->fault}<br>\n";
echo "<P>Code: {$client->faultcode}<br>\n";
echo "<P>String: {$client->faultstring}<br>\n";
echo "<P>Detail: {$client->faultdetail}<br>\n";
return;
}
// If we got to here, the report was scheduled successfully
echo "<P>There are " . count($jobid) . " elements in the response
array.<br>\n";
echo "<P>The reportJob id is $jobid<br>\n";
// Now we need to wait until the report job is ready
$jobidparam = "<reportJobId>$jobid</reportJobId>";
// It's a nusoap thing that the param string needs to include the
operation name too
$statusxml = "<getReportJobStatus
xmlns='https://adwords.google.com/api/adwords/v2'> $jobidparam
</getReportJobStatus>";
$jobstatus = $client->call("getReportJobStatus", $statusxml);
$jobstatus = $jobstatus['getReportJobStatusReturn'];
// echo "<P>There are " . count($jobstatus) . " elements in the status
array.<br>\n";
echo "Status for report job $jobid is $jobstatus<br>\n";
while((strcmp($jobstatus, "InProgress") == 0) || (strcmp($jobstatus,
"Pending") == 0)) {
// check the status every 30 seconds
sleep(30);
$jobstatus = $client->call("getReportJobStatus", $statusxml);
$jobstatus = $jobstatus['getReportJobStatusReturn'];
echo "Status for report job $jobid is $jobstatus<br>\n";
}
echo "<P>No longer waiting, check if the report is ready or
failed</P><br>\n";
if(strcmp($jobstatus, "Failed") == 0) {
echo "The report generation process failed, sorry!<br>\n";
return;
}
// The report job completed so get the URL to the report and download
it
// NOTE: You must have the CURL package installed for this to work
// see http://curl.haxx.se/
if(strcmp($jobstatus, "Completed") == 0) {
$downloadURLxml = "<getReportDownloadUrl
xmlns='https://adwords.google.com/api/adwords/v2'> $jobidparam
</getReportDownloadUrl>";
$reportURL = $client->call("getReportDownloadUrl", $downloadURLxml);
$reportURL = $reportURL['getReportDownloadUrlReturn'];
echo "<P>Download URL is $reportURL<br>\n";
// Make sure the specified filename exists and is writable
$fp = fopen("report1.xml", "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $reportURL);
curl_setopt($ch, CURLOPT_FILE, $fp);
// Download the report
$result = curl_exec($ch);
curl_close($ch);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
</body>
</html>