V5.1 Ad Group Targeting Sample in PHP

Hello.

Below is an ad group targeting sample written in PHP that uses the V5.1 Campaign Management Web service. We'll include this sample in a future MSDN release.

The Target Class allows you to specify target options for age ranges, days of the week, gender, hour ranges, and location. This specific sample shows how to incrementally bid for specific age ranges. Other target options would use a similar technique.

<?php

// This program requires the following PHP extensions:
// php_soap.dll
// php_openssl.dll

// To ensure that a cached WSDL is not being used,
// disable WSDL caching.
ini_set("soap.wsdl_cache_enabled", "0");

try
{

    $adGroupId = 50927722; //Application-specific value.

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

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

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

    // The name of the service operation that will be called.
$action = "AddTarget";   

    // The user name, password, and developer token are
// expected to be passed in as command-line
// arguments.
// $argv[0] is the PHP file name.
// $argv[1] is the user name.
// $argv[2] is the password.
// $argv[3] is the developer token.
if ($argc !=4)
{
printf("Usage:\n");
printf(
"php file.php username password devtoken\n");
exit(0);
}
$username = $argv[1];
$password = $argv[2];
$developerTokenValue = $argv[3];
$applicationTokenValue="";

    // Assign the credentials to the classes
// that are used by the SOAP headers.
$userCredentials=new UserCredentials();
$userCredentials->Username=$username;
$userCredentials->Password=$password;
$developerToken=new DeveloperToken();
$developerToken->Value=$developerTokenValue;
$applicationToken=new ApplicationToken();
$applicationToken->Value=$applicationTokenValue;

    // Create the SOAP headers.
$headerApplicationToken =
new SoapHeader
(
$xmlns,
'ApplicationToken',
$applicationToken,
false
);
$headerDeveloperToken =
new SoapHeader
(
$xmlns,
'DeveloperToken',
$developerToken,
false
);
$headerUserCredentials =
new SoapHeader
(
$xmlns,
'UserCredentials',
$userCredentials,
false
);

    // Create the SOAP input header array.
$inputHeaders = array
(
$headerApplicationToken,
$headerDeveloperToken,
$headerUserCredentials
);

    // Create the SOAP client.
$opts = array('trace' => true);
$client = new SOAPClient($campaignProxy, $opts);

    // Populate an array of age targets, for incremental bids.
// Incrementally bid 10% on the 18-25–year age group.
$ageTargetBidArray[] = array(
"IncrementalBid" => 'TenPercent',
"Age" => 'EighteenToTwentyFive'
);

    // Incrementally bid 20% on the 25-35–year age group.
$ageTargetBidArray[] = array(
"IncrementalBid" => 'TwentyPercent',
"Age" => 'TwentyFiveToThirtyFive'
);

    // Incrementally bid 20% on the 35-50–year age group.
$ageTargetBidArray[] = array(
"IncrementalBid" => 'TwentyPercent',
"Age" => 'ThirtyFiveToFifty'
);

    // Incrementally bid 20% on the 50-65–year age group.
$ageTargetBidArray[] = array(
"IncrementalBid" => 'TwentyPercent',
"Age" => 'FiftyToSixtyFive'
);

     // Incrementally bid 30% on the 65-year-and-over age group.
$ageTargetBidArray[] = array(
"IncrementalBid" => 'ThirtyPercent',
"Age" => 'SixtyFiveAndAbove'
);

     // Assign the age target details
// array to an ageTarget variable.
$ageTarget = array
(
"Bids" => $ageTargetBidArray
);

// Assign the ageTarget variable to
// a target variable.
$target = array
(
"Age" => $ageTarget
);

    // Specify the parameters for the SOAP call.
$params = array
(
'AdGroupId'=>$adGroupId,
'Target'=>$target
);

    // Execute the SOAP call.
$result = $client->__soapCall
(
$action,
array( $action.'Request' => $params ),
null,
$inputHeaders,
$outputHeaders
);

    print "$action succeeded with Tracking ID "
. $outputHeaders['ApiCallTrackingData']->TrackingId
. ".\n";

    // Retrieve the ad group IDs.
if (isset(
$result->AdGroupIds->int
))
{
if (is_array($result->AdGroupIds->int))
{
// An array of ad group IDs has been returned.
$obj = $result->AdGroupIds->int;
}
else
{
// A single ad group ID has been returned.
$obj = $result->AdGroupIds;
}
print "The following ad groups were returned by $action:\n";
foreach ($obj as $adGroupId)
{
print "Ad group ID: " . $adGroupId . "\n";
}
}
}

catch (Exception $e)
{
print "$action failed.\n";
// Display the fault code and the fault string.
print $e->faultcode . " " . $e->faultstring . ".\n";

    print "TrackingID: " .
$e->detail->ApiFaultDetail->TrackingId . ".\n";

    // Process operation errors.
if (isset(
$e->detail->ApiFaultDetail->OperationErrors->OperationError
))
{
if (is_array(
$e->detail->ApiFaultDetail->OperationErrors->OperationError
))
{
// An array of operation errors has been returned.
$obj =
$e->detail->ApiFaultDetail->OperationErrors->OperationError;
}
else
{
// A single operation error has been returned.
$obj = $e->detail->ApiFaultDetail->OperationErrors;
}
foreach ($obj as $operationError)
{
print "Operation error " .
$operationError->Code . " encountered. ";
print $operationError->Message . "\n";
}
}

    // Process batch errors.
if (isset(
$e->detail->ApiFaultDetail->BatchErrors->BatchError
))
{
if (is_array(
$e->detail->ApiFaultDetail->BatchErrors->BatchError
))
{
// An array of batch errors has been returned.
$obj = $e->detail->ApiFaultDetail->BatchErrors->BatchError;
}
else
{
// A single batch error has been returned.
$obj = $e->detail->ApiFaultDetail->BatchErrors;
}
foreach ($obj as $batchError)
{
print "Ad group index " . $batchError->Index . "\n";
print "Batch error " .
$batchError->Code . " encountered. ";
print $batchError->Message . "\n";
}
}
}

// Definitions for classes that are used by the SOAP headers.
class ApplicationToken
{
public $Value;
}
class DeveloperToken
{
public $Value;
}
class UserCredentials
{
public $Password;
public $Username;
}

?>

Thank you.

Walter Poupore
Lead Programming Writer
Microsoft adCenter API

If you have questions or comments about this post, you can discuss it in the API forums:

Blog Post: New ad group targeting sample in PHP using the V5.1 Ca...
 

 https://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3499272&SiteID=1&mode=1