Section 14.5. Adding an Ad


14.5. Adding an Ad

The CreativeService Google AdWords API web service is used to add an ad to an AdGroup. It works in much the same way as the other Google AdWords API services, such as the AdGroupService and the CampaignService web services.

Here are the steps you need to follow to create an ad:

  1. Create a client object.

  2. Use the header information to authenticate the client object.

  3. Generate (or retrieve) the fields of the ad, including the identification for the ad group, and wrap the fields in appropriate XML.

  4. Make the request to add the creative.

14.5.1. Creating a Client Object and Setting Headers

Once again, the WSDL file is used to create a new SOAP client object, this time using the CreativeService:

     $creativewsdl = "https://adwords.google.com/api/adwords/v2/CreativeService?wsdl";     $creativeclient = new soapclient($creativewsdl, 'wsdl');

The authentication header that was constructed and saved via session tracking is used to set the header for the service:

     $creativeclient->setHeaders($header);

14.5.2. Generating the Creative and Making the Request

In a real-life application, you might get a fair amount of whiz-bang functionality out of generating portions of a creative programmatically. For example, the text of an ad could respond to internal inventory conditions and be automatically generated.

For this example, to keep things relatively simple, the creative is generated from the user's input in the form shown earlier. It can be picked up using HTTP POST:

     $headline = $_POST['headline'];

The other elements of the ad are obtaining similarly (see Example 14-5) and each element in the creative is then wrapped in XML. For example:

     $headline = makeDocLit ("headline", $headline);

The creative is then constructed by putting all the elements together along with the AdGroup ID, which is retrieved using session tracking:

     $creative1 = "<creative>$adgroupid $headline $description1 $description2        $destinationUrl $displayUrl </creative>";     $creativeparamsxml = "<addCreative> $creative1 </addCreative>";

The creative is next added to the AdGroup:

     $creativesarray = $creativeclient->call("addCreative", $creativeparamsxml);     $creativesarray = $creativesarray['addCreativeReturn'];

If there are any errors, they should be displayed and the program halted:

     if($creativeclient->fault) {        showErrors($creativeclient);     echo '<a href="authenticate.php">Try again</a>';     exit;     }

Otherwise, if there are no errors, the keywords can be added to the AdGroup.



Google Advertising Tools. Cashing in with AdSense, AdWords, and the Google APIs
Google Advertising Tools: Cashing in with Adsense, Adwords, and the Google APIs
ISBN: 0596101082
EAN: 2147483647
Year: 2004
Pages: 145
Authors: Harold Davis

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net