How to Create Keywords 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.


The following PHP example shows how to create keywords by using the Campaign Management Web service. This example assumes that you have already determined which ad group ID will be used for the keywords; you must substitute your ad group ID for the $adGroupID variable that is assigned 50786905 in the following code. 


Thanks,
Walter Poupore
Lead Programming Writer
Microsoft adCenter API


<?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 = 50786905;  //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 Microsoft 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 = "AddKeywords";


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


    $negativeKeywords = array
    (
        'latex',
        'boxing',
        'softball',
        'baseball'
    );


    // Create an array of keywords.
    $keywords[] = array
    (
        BroadMatchBid => 0.50,
        ContentMatchBid => 0.50,
        ExactMatchBid => 0.50,
        NegativeKeywords => null,
        Param1 => null,
        Param2 => null,
        Param3 => null,
        Text => 'mittens'
    );


    $keywords[] = array
    (
        BroadMatchBid => 0.50,
        ContentMatchBid => 0.50,
        ExactMatchBid => 0.50,
        NegativeKeywords => $negativeKeywords,
        Param1 => null,
        Param2 => null,
        Param3 => null,
        Text => 'gloves'
    );



    // Specify the parameters for the SOAP call.
    $params = array
    (
        'AdGroupId' => $adGroupId,
        'Keywords' => $keywords
    );
   
    // 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 keyword IDs.
    if (isset(
        $result->KeywordIds->int
        ))
    {
        if (is_array($result->KeywordIds->int))
        {
            // An array of keyword IDs has been returned.
            $obj = $result->KeywordIds->int;
        }
        else
        {
            // A single keyword ID has been returned.
            $obj = $result->KeywordIds;
        }
        print "The following keyword IDs were returned by $action:\n";
        foreach ($obj as $keywordId)
        {
            print "Keyword ID: " . $keywordId . "\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->EditorialApiFaultDetail->TrackingId . ".\n";


    // Process any editorial validation errors.
    if (isset(
        $e->detail->EditorialApiFaultDetail->EditorialErrors->EditorialError
        ))
    {
        if (is_array(
            $e->detail->EditorialApiFaultDetail->
            EditorialErrors->EditorialError
            ))
        {
            // An array of editorial validation errors has been returned.
            $obj =
                $e->detail->EditorialApiFaultDetail->
                EditorialErrors->EditorialError;
        }
        else
        {
            // A single editorial validation error has been returned.
            $obj = $e->detail->EditorialApiFaultDetail->EditorialErrors;
        }
        foreach ($obj as $evError)
        {
            print "Editorial error " .
                $evError->Code . " encountered. ";
            print "Failed text: " . $evError->DisapprovedText . "\n";
            print $evError->Message . "\n";
        }
    }


    // Process any operation errors.
    if (isset(
        $e->detail->EditorialApiFaultDetail->OperationErrors->OperationError
        ))
    {
        if (is_array(
            $e->detail->EditorialApiFaultDetail->OperationErrors->OperationError
            ))
        {
            // An array of operation errors has been returned.
            $obj =
                $e->detail->EditorialApiFaultDetail->OperationErrors->OperationError;
        }
        else
        {
            // A single operation error has been returned.
            $obj = $e->detail->EditorialApiFaultDetail->OperationErrors;
        }
        foreach ($obj as $operationError)
        {
            print "Operation error " .
                $operationError->Code . " encountered. ";
            print $operationError->Message . "\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 "Keyword 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;
}


?>

Comments (4)

  1. Cnorred says:

    i wanna create key words of my blog any one tell me how i can?

  2. Anonymous says:

    In recent years, due to price competition and new technologies, the need for new models development, the market had a kind of down + cotton as a filler of the ?down coat?, a certain percentage of down (the sum of velvet flowers and raw footage) plus on a certain percentage of cotton as a filler to reduce costs.

    Though there are many diverse makes and styles from the arctic parka jacket, you do should take into account your finances. When you are getting a single for each day dress in then you certainly might not need to spend a fantastic deal and you will find reasonably priced helps make around the promote.You will have to have a look at the size with the jacket and the lining as the two of those will help to maintain you warm. In case the jacket is too small then it can journey up and allow the cold air in. No matter what brand name or type you decide on, you recognize that for anyone who is shopping for an arctic parka jacket which you are dressed for winter season.

    <a href="http://www.canadagoosesuper.com/">Canada Goose Super</a> Jackets On Sale did have a decline while in the trend sector and people have been believed for being unfashionable in the event you were noticed with one on. This even so has now transformed and much more and more individuals are seeing them for that amazing fashion style they are.

    <a href="http://www.canadagoosesuper.com/Canada-Goose-Mens-Parka-C3.htm">Canada Goose Mens parka</a> Sale is essential to much now how much extra extra cold winter, especially living in the real cold weather. Everybody love and warmth of the elements, but we usually discouraged, the best strategy inside cleaning and clothing crammed down immediately after rapid emergence. Incorrect cleaning methods may destroy the fluffy feather may clothes, the base of the warmth, <a href="http://www.canadagoosesuper.com/Canada-Goose-Lovers-Jacket-C21.htm">Canada Goose Lovers jacket</a> can be designed to use it with a very small dirty down.

Skip to main content