How to Check the Status of an Ad Group in PHP (V5)


We will release PHP samples in V5 to MSDN by May 2008. In the interim, we'll use this site to release PHP samples in V5.


 After submitting an ad group for review, you can check the status of the ad group by calling the GetKeywordAdAssociationsByAdIds, GetKeywordAdAssociationsByKeywordIds, or GetKeywordAdAssociationsByStatus service operation. The following PHP examples show how to use these service operations to check the status of an ad group. These examples assume that you have already submitted an ad group for review. These examples also assume that you have already determined which ad group IDs, ad IDs, and keyword IDs will be checked; you must substitute your own values for the ad group IDs, ad IDs, and keyword IDs used in the following code.


Thanks,
Walter Poupore
Lead Programming Writer
Microsoft adCenter API


Check the Status of an Ad Group by Using GetKeywordAdAssociationsByAdIds


<?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 = 50787199;  //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/";
    // The following commented-out line contains the production URI.
    //$URI = "https://adcenterapi.microsoft.com/api/advertiser/v5/";


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


    // 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
    );
   
    $adIds = array
    (
        164481,
        258488,
    );


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


    // Specify the parameters for the SOAP call.
    $params = array
    (
        'AdGroupId' => $adGroupId,
        'AdIds' => $adIds
    );
   
    // 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";
    
    if (isset($result->KeywordAdAssociations->ArrayOfKeywordAdAssociation))
    {
        $associationsArray = $result->KeywordAdAssociations->ArrayOfKeywordAdAssociation;
       
        // Ensure that ArrayOfKeywordAdAssociations is an array.
        if ( 2 > sizeof($associationsArray))
        {
            // Only a single object, not an array, was returned.
            // Convert the object into an array.
            $temp = array();
            $temp[] = $associationsArray;
            $associationsArray = array();
            $associationsArray = $temp;
        }
       
        // Process each array of keyword ad associations.
        for ($i = 0; $i < sizeof($associationsArray); $i++)
        {       
            $associations = $associationsArray[$i]->KeywordAdAssociation;
            if ( 2 > sizeof($associations))
            {
                // Only a single object, not an array, was returned.
                // Convert the object into an array.
                $temp = array();
                $temp[] = $associations;
                $associations = array();
                $associations = $temp;
            }
           
            // Print each keyword ad association.
            foreach ($associations as $association)
            {
                if (!isset($association->KeywordId))
                {
                    // There was no association returned.
                    continue;
                }
               
                // Print out the keyword ID, ad ID, and status.
                print "Keyword ID $association->KeywordId ";
                print "associated with ad ID $association->AdId ";
                print "has status $association->Status.\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 "Campaign 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;
}


?> 


Check the Status of an Ad Group by Using GetKeywordAdAssociationsByKeywordIds


<?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 = 50787199;  //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/";
    // The following commented-out line contains the production URI.
    //$URI = "https://adcenterapi.microsoft.com/api/advertiser/v5/";


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


    // 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
    );
   
    $keywordIds = array
    (
        58528531,
        58527999,
        58528000,
        58528001
    );


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


    // Specify the parameters for the SOAP call.
    $params = array
    (
        'AdGroupId' => $adGroupId,
        'KeywordIds' => $keywordIds
    );
   
    // 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";
    
    if (isset($result->KeywordAdAssociations->ArrayOfKeywordAdAssociation))
    {
        $associationsArray = $result->KeywordAdAssociations->ArrayOfKeywordAdAssociation;
       
        // Ensure that ArrayOfKeywordAdAssociations is an array.
        if ( 2 > sizeof($associationsArray))
        {
            // Only a single object, not an array, was returned.
            // Convert the object into an array.
            $temp = array();
            $temp[] = $associationsArray;
            $associationsArray = array();
            $associationsArray = $temp;
        }
       
        // Process each array of keyword ad associations.
        for ($i = 0; $i < sizeof($associationsArray); $i++)
        {       
            $associations = $associationsArray[$i]->KeywordAdAssociation;
            if ( 2 > sizeof($associations))
            {
                // Only a single object, not an array, was returned.
                // Convert the object into an array.
                $temp = array();
                $temp[] = $associations;
                $associations = array();
                $associations = $temp;
            }
           
            // Print each keyword ad association.
            foreach ($associations as $association)
            {
                if (!isset($association->KeywordId))
                {
                    // There was no association returned.
                    continue;
                }
               
                // Print out the keyword ID, ad ID, and status.
                print "Keyword ID $association->KeywordId ";
                print "associated with ad ID $association->AdId ";
                print "has status $association->Status.\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 "Campaign 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;
}


?> 


 Check the Status of an Ad Group by Using GetKeywordAdAssociationsByStatus


<?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 = 50787199;  //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/";
    // The following commented-out line contains the production URI.
    //$URI = "https://adcenterapi.microsoft.com/api/advertiser/v5/";


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


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


    // This sample checks for the Active status.
    // Update your code as needed to check for
    // different statuses.
    $status = "Active";


    // Show all status changes since Jan. 1, 2008, at 12:00 P.M. (noon).
    $modifiedAfter = '2008-01-01T12:00:00.000Z';


    // Specify the parameters for the SOAP call.
    $params = array
    (
        'AdGroupId'     => $adGroupId,
        'ModifiedAfter' => $modifiedAfter,
        'Status'        => $status
    );
   
    // 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";
   
    if (isset($result->KeywordAdAssociations))
    {
        $associations = $result->KeywordAdAssociations->KeywordAdAssociation;
       
        // Ensure that $associations is an array.
        if ( 2 > sizeof($associations))
        {
            // Only a single object, not an array, was returned.
            // Convert the object into an array.
            $temp = array();
            $temp[] = $associations;
            $associations = array();
            $associations = $temp;
        }
       
        // Print each keyword ad association.
        foreach ($associations as $association)
        {
            if (!isset($association->KeywordId))
            {
                // There was no association returned.
                continue;
            }
           
            // Print out the keyword ID, ad ID, and status.
            print "Keyword ID $association->KeywordId ";
            print "associated with ad ID $association->AdId ";
            print "has status $association->Status.\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";
        }
    }


}


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


?>


 

Comments (1)

  1. Cnorred says:

    Pingback from  Walter Poupore’s Blog: Microsoft adCenter and PHP | Development Blog With Code Updates : Developercast.com

Skip to main content