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 identifier will be used for the keywords; you must substitute your ad group identifier for the $adGroupID variable in the following code.
For an example about creating an ad group and retrieving the ad group identifier, see Create Ad Groups in Perl.
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
{
# Use either the sandbox or the production URI.
# This example is for the sandbox URI.
my $URI = "https://sandboxapi.adcenter.microsoft.com/api/advertiser/v6";
# The following commented-out line contains the production URI.
#my $URI = "https://adcenterapi.microsoft.com/api/advertiser/v6";
# The Microsoft adCenter API namespace.
my $xmlns = "https://adcenter.microsoft.com/api/advertiser/v6";
# The common data-type namespace.
my $namespaceArrays =
"http://schemas.microsoft.com/2003/10/Serialization/Arrays";
# 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);
my $username = "username";
my $password = "password";
my $devToken = "devtoken";
my $appToken = "apptoken";
my $customerAccountId = 1234;
my $customerId = 5678;
my $adGroupId = 123456;
# Assemble the service operation parameters.
my @header = CreateV6Header(
$username,
$password,
$devToken,
$appToken,
$customerAccountId,
$customerId);
my @negativeKeywords = SOAP::Data->name(NegativeKeywords =>
\SOAP::Data->value
(
SOAP::Data->name(string => 'latex'),
SOAP::Data->name(string => 'boxing'),
SOAP::Data->name(string => 'softball'),
SOAP::Data->name(string => 'baseball')
))->attr({'xmlns:a' => $namespaceArrays});
my @Keywords = SOAP::Data->name(Keywords => \SOAP::Data->value
(
SOAP::Data->name(Keyword => \SOAP::Data->value
(
SOAP::Data->name(BroadMatchBid => \SOAP::Data->value
(
SOAP::Data->name(Amount => 0.50)
)),
SOAP::Data->name(ContentMatchBid => \SOAP::Data->value
(
SOAP::Data->name(Amount => 1.00)
)),
SOAP::Data->name(ExactMatchBid => \SOAP::Data->value
(
SOAP::Data->name(Amount => 1.50)
)),
SOAP::Data->name(NegativeKeywords => '')
->attr({'xmlns:a' => $namespaceArrays}),
SOAP::Data->name(Param1 => ''),
SOAP::Data->name(Param2 => ''),
SOAP::Data->name(Param3 => ''),
SOAP::Data->name(Text => 'mittens')
)),
SOAP::Data->name(Keyword => \SOAP::Data->value
(
SOAP::Data->name(BroadMatchBid => \SOAP::Data->value
(
SOAP::Data->name(Amount => 10.50)
)),
SOAP::Data->name(ContentMatchBid => \SOAP::Data->value
(
SOAP::Data->name(Amount => 11.00)
)),
SOAP::Data->name(ExactMatchBid => \SOAP::Data->value
(
SOAP::Data->name(Amount => 12.50)
)),
SOAP::Data->name(NegativeKeywords => @negativeKeywords),
SOAP::Data->name(Param1 => ''),
SOAP::Data->name(Param2 => ''),
SOAP::Data->name(Param3 => ''),
SOAP::Data->name(Text => 'gloves')
))
))->attr({'xmlns:i' => 'http://www.w3.org/2001/XMLSchema-instance'});
my @params =
(
@header,
SOAP::Data->name("AdGroupId" => $adGroupId),
@Keywords
);
my $method = SOAP::Data->name
(
$action.'Request'
)-> attr({xmlns => $xmlns});
my $response =
$campaignManagementService->call
(
$method => @params
);
# Check for errors.
if ($response->fault)
{
print "$action failed.\n";
# Display the adCenter tracking identifier.
my $trackingId = $response->valueof
(
'//AdApiFaultDetail/TrackingId'
);
print "TrackingId: $trackingId\n";
# Display any generic errors.
my @standardErrors = $response->valueof
(
'//AdApiFaultDetail/Errors/AdApiError'
);
foreach my $standardError (@standardErrors)
{
print "Standard error ($standardError->{Code}) encountered. ";
print "$standardError->{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 editorial errors.
my @editorialErrors = $response->valueof
(
'//EditorialApiFaultDetail/EditorialErrors/EditorialError'
);
foreach my $editorialError (@editorialErrors)
{
print "Operation error ($editorialError->{Code}) encountered. ";
print "$editorialError->{Message}\n";
print "$editorialError->{DisapprovedText}\n";
}
# Display any batch errors.
my @batchErrors = $response->valueof
(
'//EditorialApiFaultDetail/BatchErrors/BatchError'
);
foreach my $batchError (@batchErrors)
{
print "Campaign 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 identifier.
my $trackingId = $response->valueof
(
'//TrackingId'
);
print "TrackingId: $trackingId\n";
# Display the keyword identifiers.
my @ids = $response->valueof
(
'//KeywordIds/long'
);
print "The following keyword identifiers were returned by $action:\n";
foreach my $longval (@ids)
{
print "Keyword ID: $longval\n";
}
}
sub CreateV6Header
{
# The method parameters.
my (
$username,
$password,
$devtoken,
$apptoken,
$customeraccountid,
$customerid) = @_;
# Initialize the application token.
my $AppToken = SOAP::Header->name
(
"ApplicationToken"=>$apptoken
)->attr({xmlns => $xmlns});
# Initialize the developer token.
my $DevToken =SOAP::Header->name
(
"DeveloperToken"=>$devtoken
)->attr({xmlns => $xmlns});
# Initialize the user name.
my $UserName =SOAP::Header->name
(
"UserName"=>$username
)->attr({xmlns => $xmlns});
# Initialize the password.
my $Password =SOAP::Header->name
(
"Password"=>$password
)->attr({xmlns => $xmlns});
# Initialize the customer account identifier.
my $CustomerAccountId =SOAP::Header->name
(
"CustomerAccountId"=>$customeraccountid
)->attr({xmlns => $xmlns});
# Initialize the customer identifier.
my $CustomerId =SOAP::Header->name
(
"CustomerId"=>$customerid
)->attr({xmlns => $xmlns});
# Assemble the header parameters.
my @headerParams =
(
$AppToken,
$CustomerAccountId,
$CustomerId,
$DevToken,
$Password,
$UserName,
);
return @headerParams;
}
};
warn $@ if $@;
Each successfully created keyword is assigned a keyword identifier by Microsoft adCenter. The keyword identifier is stored in the Id element of the Keyword object.
When you have received the keyword identifiers, you should store them for future use. For information about why storing the identifiers 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 elements that are 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 Submit an Ad Group for Approval in Perl.
Concepts
Create Ads in Perl
Create Ad Groups in Perl
Submit an Ad Group for Approval in Perl
Create Campaigns in Perl
Hello Microsoft adCenter Sample in Perl