Home > adCenter API Blog for Developers > adCenter API Blog for Developers Blog Subscribe RSS

  • Hello adCenter V5 sample written in PHP Monday, March 17, 2008 by: Walter Poupore - MSFT 0 Comments

    Hello!

    We've prepared Microsoft adCenter API Version 5 (V5) samples written in PHP for a future release to MSDN. In the interim, we're providing the PHP sample below.

    This example assumes that you have already determined your account ID; you must substitute your account ID for the $accountId variable that is assigned 489 in the following code.

    When you call your code, pass in the API credentials as command-line parameters. In the code, $argv represents the name of the PHP file and your API credentials. $argv[0] is the name of the PHP file; $argv[1] is your user name; $argv[2] is your password; $argv[3] is your developer token. For example, if your PHP script is named HelloAdCenterV5.PHP, execute the script as follows.

    php HelloAdCenterV5.php your_user_name your_password your_developer_token

    Substitute your API credentials for the placeholders your_user_name, your_password, and your_developer_token.

    Thank you.

    Walter Poupore
    Lead Programming Writer
    Microsoft adCenter API

    Get Campaigns for an Account

    <?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
    {

        $accountId = 489;  //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 = "GetCampaignsByAccountId";

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

        // Specify the parameters for the SOAP call.
        $params = array
        (
            'AccountId'=>$accountId
        );
      
        // 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 campaigns.
        $campaigns = array();
        $campaigns=$result->Campaigns;
       
        if (isset(
            $campaigns->Campaign
            ))
        {
            if (is_array($campaigns->Campaign))
            {
                // An array of campaigns has been returned.
                $obj = $campaigns->Campaign;
            }
            else
            {
                // A single campaign has been returned.
                $obj = $campaigns;
            }
            print "The following campaigns were returned by $action.\n";
            foreach ($obj as $campaign)
            {
                print "Campaign Id: " .
                      $campaign->Id .
                      " Name: " .
                      $campaign->Name .
                      "\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;
    }

    ?>

0 Comments RSS

  • No Comments

0 Trackbacks

  • No Trackbacks