Get Started Using PHP with Bing Ads Services

To get started developing Bing Ads applications with PHP, you can start with the provided examples or follow one of the application walkthroughs for a Web or Desktop application. The examples have been developed with the Bing Ads PHP SDK and run with PHP 5.6.30. You should be able to use other versions of PHP, packages, and operating systems. However, certain parts of the code and configuration might have to be changed. For information about how to set up a PHP development environment to use web services, see the documentation for your tools. The SOAP and OpenSSL extensions should also be enabled in the PHP.ini file. Enable the curl extension to run the bulk upload samples.


You will need user credentials with access to Bing Ads either in production or sandbox. For the production environment you will need a production developer token. All sandbox clients can use the universal sandbox developer token i.e., BBD37VB98. For more information, please see Get Started With the Bing Ads API and Sandbox.

To authenticate with a Microsoft Account (email address username) in production, you must also must register an application and get the corresponding client identifier. You also need to take note of the client secret and redirect URI if you are developing a web application. For authentication details, see Authentication With the SDKs.

Install the SDK

You can install the Bing Ads PHP SDK using the Composer package manager to fetch from Packagist, or you can clone the source from GitHub. This guide describes how you can use Composer to get the latest version of the Bing Ads PHP SDK.

  1. Download and install Composer. Microsoft Windows users should also add composer.phar to your PATH variable.

  2. Open a command prompt and type composer require microsoft/bingads.


    Windows users who did not add composer.phar to the PATH will need to type php composer.phar require microsoft/bingads instead.

  3. To get updates going forward, type composer update. If any updates are available at packagist, composer will install the latest version.


Once you have the Bing Ads PHP SDK installed you can either browse the Bing Ads Code Examples, download the examples at GitHub, or follow one of the application walkthroughs for a Web or Desktop application.

Using SoapVar

To send a complex type that inherits from a base class, you must encode the object as a SoapVar. In the following example the BiddableCampaignCriterion (derived from CampaignCriterion), LocationCriterion (derived from Criterion), and BidMultiplier (derived from CriterionBid) are all encoded.

$campaignCriterions = array();

$locationBiddableCampaignCriterion = new BiddableCampaignCriterion();
$locationBiddableCampaignCriterion->CampaignId = $campaignId;
$locationCriterion = new LocationCriterion();
$locationCriterion->LocationId = 190;
$encodedLocationCriterion = new SoapVar($locationCriterion, SOAP_ENC_OBJECT, 'LocationCriterion', $GLOBALS['CampaignProxy']->GetNamespace());
$locationBiddableCampaignCriterion->Criterion = $encodedLocationCriterion;
$bidMultiplier = new BidMultiplier();
$bidMultiplier->Multiplier = 0;
$encodedBidMultiplier = new SoapVar($bidMultiplier, SOAP_ENC_OBJECT, 'BidMultiplier', $GLOBALS['CampaignProxy']->GetNamespace());
$locationBiddableCampaignCriterion->CriterionBid = $encodedBidMultiplier;

$encodedCriterion = new SoapVar($locationBiddableCampaignCriterion, SOAP_ENC_OBJECT, 'BiddableCampaignCriterion', $GLOBALS['CampaignProxy']->GetNamespace());
$campaignCriterions[] = $encodedCriterion;

If you do not encode the CriterionBid as concrete type BidMultiplier, the generated request SOAP is an empty abstract CriterionBid element. This will result in a serialization exception with the message "Cannot create an abstract class."


The object must be properly encoded with the complex type e.g., BidMultiplier.

<ns1:CriterionBid xsi:type="ns1:BidMultiplier">
    <ns1:Type xsi:nil="true" />

Reference documentation for each data object specifies whether or not the complex type is derived from a base type e.g., please see BiddableCampaignCriterion, LocationCriterion, and BidMultiplier.

The WSDL also defines the hierarchy e.g., the following definition for BidMultiplier.

<xs:complexType name="BidMultiplier">
  <xs:complexContent mixed="false">
    <xs:extension base="tns:CriterionBid">
        <xs:element minOccurs="0" name="Multiplier" type="xs:double"/>
<xs:element name="BidMultiplier" nillable="true" type="tns:BidMultiplier"/>

See Also

Bing Ads Client Libraries
Bing Ads Code Examples
Bing Ads Web Service Addresses
Handling Service Errors and Exceptions