Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Add Targets in PHP

The following example shows how to add a target to a customer’s library that can be used to incrementally increase bids based on age, day, gender, hour, and location using the Campaign Management AddTargetsToLibrary service operation.

This example has been developed and run within the environment described in Getting Started Using PHP with Bing Ads Services.

To download the PHP classes included in the sample, see PHP Production Classes for Bing Ads API.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.

<?php

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

try
{

    // Application-specific values.
    $accountId = <youraccountid>;
    $customerId = <yourcustomerid>;
    
    // Use either the sandbox or the production URI.
    // This example is for the sandbox URI.
    $URI =
        "https://api.sandbox.bingads.microsoft.com/Api/Advertiser/v8/";
    // The following commented-out line contains the production URI.
    //$URI = "https://adcenterapi.microsoft.com/api/advertiser/v8/";

    // The Bing Ads API namespace.
    $xmlns = "https://adcenter.microsoft.com/v8";

    // 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 = "AddTargetsToLibrary";

    // 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];
    $developerToken = $argv[3];
    $applicationToken = "";

    // Create the SOAP headers.
    $headerApplicationToken = 
        new SoapHeader
        (
            $xmlns,
            'ApplicationToken',
            $applicationToken,
            false
        );
    $headerDeveloperToken = 
        new SoapHeader
        (
            $xmlns,
            'DeveloperToken',
            $developerToken,
            false
        );
    $headerUserName = 
        new SoapHeader
        (
            $xmlns,
            'UserName',
            $username,
            false
        );
    $headerPassword = 
        new SoapHeader
        (
            $xmlns,
            'Password',
            $password,
            false
        );

    $headerCustomerAccountId = 
        new SoapHeader
        (
            $xmlns,
            'CustomerAccountId',
            $accountId,
            false
        );

    $headerCustomerId = 
        new SoapHeader
        (
            $xmlns,
            'CustomerId',
            $customerId,
            false
        );

    // Create the SOAP input header array.
    $inputHeaders = array
    (
        $headerApplicationToken,
        $headerDeveloperToken,
        $headerUserName,
        $headerPassword,
        $headerCustomerAccountId,
        $headerCustomerId
    );

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

    $targets = array();

    // This example sets up a target for Age, Day, 
    // Gender, Hour, and State
    $targets[0] = new Target();
    
    // Bid incrementally on 2 age ranges.
    $ageTargetBids = array();
    $ageTargetBids[0] = new AgeTargetBid();
    $ageTargetBids[0]->Age = 'EighteenToTwentyFive';
    $ageTargetBids[0]->IncrementalBid = 'EightyPercent';
    $ageTargetBids[1] = new AgeTargetBid();
    $ageTargetBids[1]->Age = 'TwentyFiveToThirtyFive';
    $ageTargetBids[1]->IncrementalBid = 'FiftyPercent';

    // Bid incrementally on Friday and Saturday.
    $dayTargetBids = array();
    $dayTargetBids[0] = new DayTargetBid();
    $dayTargetBids[0]->Day = 'Friday';
    $dayTargetBids[0]->IncrementalBid = 'TwentyPercent';    
    $dayTargetBids[1] = new HourTargetBid();
    $dayTargetBids[1]->Day = 'Saturday';
    $dayTargetBids[1]->IncrementalBid = 'ThirtyPercent';    

    // Bid incrementally on gender.
    $genderTargetBids = array();
    $genderTargetBids[0] = new GenderTargetBid();
    $genderTargetBids[0]->Gender = 'Male';
    $genderTargetBids[0]->IncrementalBid = 'TenPercent';    
    $genderTargetBids[1] = new GenderTargetBid();
    $genderTargetBids[1]->Gender = 'Female';
    $genderTargetBids[1]->IncrementalBid = 'ThirtyPercent';    

    // Bid incrementally on several hour ranges.
    $hourTargetBids = array();
    $hourTargetBids[0] = new HourTargetBid();
    $hourTargetBids[0]->Hour = 'TwoPMToSixPM';
    $hourTargetBids[0]->IncrementalBid = 'FortyPercent';    
    $hourTargetBids[1] = new HourTargetBid();
    $hourTargetBids[1]->Hour = 'SixPMToElevenPM';
    $hourTargetBids[1]->IncrementalBid = 'TwentyPercent';    
    $hourTargetBids[2] = new HourTargetBid();
    $hourTargetBids[2]->Hour = 'ElevenPMToThreeAM';
    $hourTargetBids[2]->IncrementalBid = 'TenPercent';

    // Bid incrementally on a state.
    $stateTargetBids = array();
    $stateTargetBids[0] = new StateTargetBid();
    $stateTargetBids[0]->State = 'US-CA';  // This bid targets California.
    $stateTargetBids[0]->IncrementalBid = 'NinetyPercent';
  
    $targets[0]->Name = 'Target #1';

    $targets[0]->Age = new AgeTarget();
    $targets[0]->Age->Bids = $ageTargetBids;
    
    $targets[0]->Day = new DayTarget();
    $targets[0]->Day->Bids = $dayTargetBids;
    $targets[0]->Day->TargetAllDays = true;
    
    $targets[0]->Gender = new GenderTarget();
    $targets[0]->Gender->Bids = $genderTargetBids;
    
    $targets[0]->Hour = new HourTarget();
    $targets[0]->Hour->Bids = $hourTargetBids;
    $targets[0]->Hour->TargetAllHours = true;
    
    $targets[0]->Location = new LocationTarget();
    $targets[0]->Location->StateTarget = new StateTarget();
    $targets[0]->Location->StateTarget->Bids = $stateTargetBids;

    // Specify the parameters for the SOAP call.
    $params = array
    (
        'Targets' => array('Target' => $targets)
    );
   
    // Execute the SOAP call.
    $result = $client->__soapCall
    (
        $action,
        array( $action.'Request' => $params ),
        null,
        $inputHeaders,
        $outputHeaders
    );
    
     print "Successful $action call.\n";
     print "TrackingId output from response header: "
          . $outputHeaders['TrackingId']
          . ".\n";
 
     // Insert a blank line to separate the text that follows.
     print "\n";
     
    // Retrieve the target IDs.
    if (isset(
        $result->TargetIds->long
        ))
    {
        if (is_array($result->TargetIds->long))
        {
            // An array of target IDs has been returned.
            $obj = $result->TargetIds->long;
        }
        else
        {
            // A single target ID has been returned.
            $obj = $result->TargetIds;
        }
        
        // Display the target IDs for the targets that were created.
        $i = 0;
        foreach ($obj as $targetId)
        {
            print "Target named '" . $targets[$i]->Name . "' was created" . 
                " with target ID $targetId.\n";
            $i = $i + 1;
            
        }
    }

}

catch (Exception $e)
{
    print "$action failed.\n";
    
    if (isset($e->detail->ApiFaultDetail))
    {
      print "ApiFaultDetail exception encountered\n";
      print "Tracking ID: " . 
          $e->detail->ApiFaultDetail->TrackingId . "\n";

      // Process any 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 encountered:\n";
              print "\tMessage: ". $operationError->Message . "\n"; 
              print "\tDetails: ". $operationError->Details . "\n"; 
              print "\tErrorCode: ". $operationError->ErrorCode . "\n"; 
              print "\tCode: ". $operationError->Code . "\n"; 
          }
      }

      // Process any 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 "Batch error encountered for array index " . $batchError->Index . ".\n";
              print "\tMessage: ". $batchError->Message . "\n"; 
              print "\tDetails: ". $batchError->Details . "\n"; 
              print "\tErrorCode: ". $batchError->ErrorCode . "\n"; 
              print "\tCode: ". $batchError->Code . "\n"; 
          }
      }
    }

    if (isset($e->detail->AdApiFaultDetail))
    {
      print "AdApiFaultDetail exception encountered\n";
      print "Tracking ID: " . 
          $e->detail->AdApiFaultDetail->TrackingId . "\n";

      // Process any operation errors.
      if (isset(
          $e->detail->AdApiFaultDetail->Errors
          ))
      {
          if (is_array(
              $e->detail->AdApiFaultDetail->Errors
              ))
          {
              // An array of errors has been returned.
              $obj = $e->detail->AdApiFaultDetail->Errors;
          }
          else
          {
              // A single error has been returned.
              $obj = $e->detail->AdApiFaultDetail->Errors;
          }
          foreach ($obj as $Error)
          {
              print "Error encountered:\n";
              print "\tMessage: ". $Error->Message . "\n"; 
              print "\tDetail: ". $Error->Detail . "\n"; 
              print "\tErrorCode: ". $Error->ErrorCode . "\n"; 
              print "\tCode: ". $Error->Code . "\n"; 
          }
      }

    }

    // Display the fault code and the fault string.
    print $e->faultcode . " " . $e->faultstring . ".\n";

    // Output the last request.
    print "Last SOAP request:\n";
    print $client->__getLastRequest() . "\n";
}

class Target
{
    public $Age;
    public $Day;
    public $Gender;
    public $Hour;
    public $Id;
    public $Location;
}

class AgeTarget
{
    public $Bids;
}

class AgeTargetBid
{
    public $Age;
    public $IncrementalBid;
}

class BusinessTarget
{
    public $Bids;
}

class BusinessTargetBid
{
    public $BusinessId;
    public $IncrementalBid;
    public $Radius;
}

class CityTarget
{
    public $Bids;
}

class CityTargetBid
{
    public $City;
    public $IncrementalBid;
}

class CountryTarget
{
    public $Bids;
}

class CountryTargetBid
{
    public $CountryAndRegion;
    public $IncrementalBid;
}


class DayTarget
{
    public $Day;
    public $IncrementalBid;
}

class DayTargetBid
{
    public $Bids;
    public $TargetAllDays;
}

class GenderTarget
{
    public $Gender;
    public $IncrementalBid;
}

class GenderTargetBid
{
    public $Bids;
}

class HourTarget
{
    public $Bids;
    public $TargetAllHours;
}

class HourTargetBid
{
    public $Hour;
    public $IncrementalBid;
}

class LocationTarget
{
    public $BusinessTarget;
    public $CityTarget;
    public $CountryTarget;
    public $MetroAreaTarget;
    public $RadiusTarget;
    public $StateTarget;
    public $TargetAllLocations;
}

class MetroAreaTarget
{
    public $Bids;
}

class MetroAreaTargetBid
{
    public $IncrementalBid;
    public $MetroArea;
}

class RadiusTarget
{
    public $Bids;
}

class RadiusTargetBid
{
    public $IncrementalBid;
    public $LatitudeDegrees;
    public $LongitudeDegrees;
    public $Radius;
}

class StateTarget
{
    public $Bids;
}

class StateTargetBid
{
    public $IncrementalBid;
    public $State;
}

?>
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.