Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Windows Live
 How to Create Keywords in Perl (V5....
Windows Live
How to Create Keywords in Perl (V5.1)

The following Perl example shows how to create keywords by using the Campaign Management Web service. This example assumes that you have already determined which ad group ID will be used for the keywords; you must substitute your ad group ID for the $AdGroupID variable that is assigned 50777472 in the following code.

For an example about creating an ad group and retrieving the ad group ID, see How to Create Ad Groups in Perl (V5.1).

package adCenterAPI;

# For release code, use the following SOAP::Lite statement.
use SOAP::Lite ( +maptype => {} );
# For debugging code, use the following commented-out SOAP::Lite statement.
#use SOAP::Lite ( +trace => all, maptype => {} );

use strict;
eval
{

my $AdGroupID = 50777472; # Application-specific value.

# Variable used to retrieve the Microsoft adCenter API tracking ID.
my $trackingId;

# Use either the sandbox or production URI.
# This example is for the sandbox URI.
my $URI = "https://sandboxapi.adcenter.microsoft.com/api/advertiser/v5.1";
# The following commented-out line contains the production URI.
#my $URI = "https://adcenterapi.microsoft.com/api/advertiser/v5.1";

# The Microsoft adCenter API namespace.
my $xmlns = "https://adcenter.microsoft.com/api/advertiser/v5";

# The proxy for the Campaign Management Web service.
my $campaignPROXY =
    $URI."/CampaignManagement/CampaignManagementService.svc?wsdl";

# The service operation that will be called.
my $action = 'AddKeywords';

# The Web service.
my $campaignManagementService = SOAP::Lite->new(
   uri => $URI, 
   proxy => $campaignPROXY, 
   on_action => ( sub { return $action } ));
$campaignManagementService->autotype(0);
$campaignManagementService->multirefinplace(1);
$campaignManagementService->readable(1);

# @ARGV is the array of credentials passed in 
# at the command line at script run time.
#   $ARGV[0] is the user name.
#   $ARGV[1] is the password.
#   $ARGV[2] is the developer token.
unless (@ARGV == 3)
{
    print "Usage: file.pl username password devtoken\n";
    die;
}
my $username = $ARGV[0];
my $password = $ARGV[1];
my $devtoken = $ARGV[2];

my $UserCreds = SOAP::Header->name
(
    "UserCredentials" => 
    {  
        Username=>SOAP::Data->name("Username" => $username),
        Password=>SOAP::Data->name("Password" => $password)
    }
)-> attr({xmlns => $xmlns});

my $AppToken = SOAP::Header->name
(
    "ApplicationToken"=>{Value =>''}
)-> attr({xmlns => $xmlns});

my $DevToken = SOAP::Header->name
(
    "DeveloperToken"=>{Value =>$devtoken}
)-> attr({xmlns => $xmlns});

my $method = SOAP::Data->name
(
    $action.'Request'
)-> attr({xmlns => $xmlns});

my $negativeKeywords = 
[
    SOAP::Data->name(string  => 'latex'),
    SOAP::Data->name(string  => 'boxing'),
    SOAP::Data->name(string  => 'softball'),
    SOAP::Data->name(string  => 'baseball')        
];

my $Keywords =
[
  SOAP::Data->name(Keyword => 
  [
    SOAP::Data->name(BroadMatchBid => 0.50),
    SOAP::Data->name(ContentMatchBid => 0.50),
    SOAP::Data->name(ExactMatchBid => 0.50),
    SOAP::Data->name(NegativeKeywords => ''),
    SOAP::Data->name(Param1 => ''),
    SOAP::Data->name(Param2 => ''),
    SOAP::Data->name(Param3 => ''),
    SOAP::Data->name(Text => 'mittens')
  ])->uri($xmlns),
  SOAP::Data->name(Keyword => 
  [
    SOAP::Data->name(BroadMatchBid => 0.50),
    SOAP::Data->name(ContentMatchBid => 0.50),
    SOAP::Data->name(ExactMatchBid => 0.50),
    SOAP::Data->name(NegativeKeywords => $negativeKeywords),
    SOAP::Data->name(Param1 => ''),
    SOAP::Data->name(Param2 => ''),
    SOAP::Data->name(Param3 => ''),
    SOAP::Data->name(Text => 'gloves')
  ])->uri($xmlns)  
];

my @keywordIds;

my @params = 
(
    $DevToken,
    $UserCreds,
    $AppToken,
    SOAP::Data->name("AdGroupId" => $AdGroupID),
    SOAP::Data->name("Keywords" => $Keywords),
    SOAP::Data->name("KeywordIds" => @keywordIds)    
    
);  

my $response = 
    $campaignManagementService->call
    (
        $method => @params
    );

# Check for errors.
if ($response->fault) 
{
    print "$action failed.\n";
    
    # Display the fault code and the fault string.
    print $response->faultcode, " ", $response->faultstring, "\n";

    # Display the adCenter tracking ID.
    my $trackingId = $response->valueof
    (
        '//EditorialApiFaultDetail/TrackingId'
    );
    print "TrackingId: $trackingId\n";

    # Display any editorial validation errors.
    my @evErrors = $response->valueof
    (
        '//EditorialApiFaultDetail/EditorialErrors/EditorialError'
    );
    foreach my $evError (@evErrors)
    {
        print "Editorial validation error";
        print " ($evError->{Code}) encountered.\n";
        print "Failed text: $evError->{DisapprovedText}\n";
        print "$evError->{Message}\n";
    }
    
    # Display any operation errors.
    my @operationErrors = $response->valueof
    (
        '//EditorialApiFaultDetail/OperationErrors/OperationError'
    );
    foreach my $operationError (@operationErrors)
    {
        print "Operation error ($operationError->{Code}) encountered. ";
        print "$operationError->{Message}\n";
    }
    
    # Display any batch errors.
    my @batchErrors = $response->valueof
    (
        '//EditorialApiFaultDetail/BatchErrors/BatchError'
    );
    foreach my $batchError (@batchErrors)
    {
        print "Ad group index $batchError->{Index}\n";
        print "Batch error ($batchError->{Code}) encountered. \n";
        print "$batchError->{Message}\n";
        print "$batchError->{Details}\n";
    }
}
else 
{
    print "$action succeeded.\n";

    # Display the adCenter tracking ID.
    my $trackingId = $response->valueof
    (
        '//ApiCallTrackingData/TrackingId'
    );
    print "TrackingId: $trackingId\n";
    print "\n";

    # Print out the ad IDs.
    my @ids = $response->valueof
    (
        '//KeywordIds/long'
    );
    print "The following keyword IDs were returned by $action:\n";
    foreach my $longval (@ids) 
    {
      print "Keyword ID: $longval \n";
    }
}
};
warn $@ if $@;

Each successfully created keyword is assigned a keyword ID by Microsoft adCenter. The keyword ID is stored in the Id element of the Keyword object.

When you have received the keyword IDs, you should store them for future use. For information about why storing the IDs is recommended, see Creating an Efficient Microsoft adCenter Application. If you don't store them, you can retrieve them by calling the GetKeywordsByAdGroupId service operation. You can retrieve the properties associated with a keyword by calling the GetKeywordsByIds service operation.

Calling GetKeywordsByAdGroupId or GetAdKeywordsByIds reduces your API quota. For more information, see Quotas in Microsoft adCenter.

When you have successfully created your ads and keywords, the next step is to submit your ad group for approval. For an example about submitting your ad group for approval, see How to Submit an Ad Group for Approval in Perl (V5.1).

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker