Export (0) Print
Expand All

Add Ads in PHP

The following example shows how to create ads within an ad group using the Campaign Management AddAds 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>;
    $adGroupId = <youradgroupid>;
    
    // 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 = "AddAds";    


    // 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
        );


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

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

    // Create an array of ads.
    $ads = array();
    $adStruct = array();

    // The first ad is a text ad.
    $ads[0] = array(
      "Title"           => 'Alpine Ski House',
      "DestinationUrl"  => 'http://www.alpineskihouse.com',
      "DisplayUrl"      => 'alpineskihouse.com',
      "Text"            => 'Alpine Ski House has a great ski selection for you.'
    );
    $adStruct[] = new SoapVar($ads[0], SOAP_ENC_OBJECT, "TextAd", $xmlns);

    // The second ad is a text ad.
    $ads[1] = array(
      "Title"           => 'Adventure Works',
      "DestinationUrl"  => 'http://www.adventure-works.com',
      "DisplayUrl"      => 'adventure-works.com',
      "Text"            => 'Adventure Works has a great selection for you.'
    );
    $adStruct[] = new SoapVar($ads[1], SOAP_ENC_OBJECT, "TextAd", $xmlns);
    
    // The third ad is a mobile ad.
    $ads[2] = array(
      "Title"           => 'Adventure Works',
      "DestinationUrl"  => 'http://www.adventure-works.com/mobile',
      "DisplayUrl"      => 'adventure-works.com',
      "Text"            => 'Shop by phone now.'
    );
    $adStruct[] = new SoapVar($ads[2], SOAP_ENC_OBJECT, "MobileAd", $xmlns);

    $params=array('AdGroupId'=>$adGroupId, 'Ads' => array('Ad' => $adStruct));
    
    // 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 ad IDs.
    if (isset(
        $result->AdIds->long
        ))
    {
        if (is_array($result->AdIds->long))
        {
            // An array of ad IDs has been returned.
            $obj = $result->AdIds->long;
        }
        else
        {
            // A single ad ID has been returned.
            $obj = $result->AdIds;
        }
        
        // Display the ad IDs for the ads that were created.
        $i = 0;
        foreach ($obj as $adId)
        {
            print "Ads[$i] was added with ID " . $adId . ".\n";
            $i = $i +1;
        }
    }
}

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

      // Process any editorial errors.
      if (isset(
          $e->detail->EditorialApiFaultDetail->EditorialErrors->EditorialError
          ))
      {
          if (is_array(
              $e->detail->EditorialApiFaultDetail->EditorialErrors->EditorialError
              ))
          {
              // An array of editorial errors has been returned.
              $obj = $e->detail->EditorialApiFaultDetail->EditorialErrors->EditorialError;
          }
          else
          {
              // A single editorial error has been returned.
              $obj = $e->detail->EditorialApiFaultDetail->EditorialErrors;
          }
          foreach ($obj as $editorialError)
          {
              print "Editorial validation error for item at array index "
                  . $editorialError->Index . "\n";
              print "\tMessage: " . $editorialError->Message . "\n"; 
              print "\tErrorCode: " . $editorialError->ErrorCode . "\n"; 
              print "\tFailed text: " . $editorialError->DisapprovedText . "\n"; 
              print "\tCode: ". $editorialError->Code . "\n"; 
          }
      }

      // Process any batch errors.
      if (isset(
          $e->detail->EditorialApiFaultDetail->BatchErrors->BatchError
          ))
      {
          if (is_array(
              $e->detail->EditorialApiFaultDetail->BatchErrors->BatchError
              ))
          {
              // An array of batch errors has been returned.
              $obj = $e->detail->EditorialApiFaultDetail->BatchErrors->BatchError;
          }
          else
          {
              // A single batch error has been returned.
              $obj = $e->detail->EditorialApiFaultDetail->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";

}

?>

Community Additions

Show:
© 2015 Microsoft