Get Started Using PHP with Bing Ads API

To get started developing Bing Ads API 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 7.0.6. You should be able to use other versions of PHP (greater than 5.6), 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.

extension=php_soap.dll
extension=php_openssl.dll
extension=php_curl.dll

You will need user credentials with access to Microsoft Advertising 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 via OAuth, you must also 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 more details about registering an application in production and the authorization code grant flow, see Authentication with OAuth and Authentication With the SDKs. Although in production you must use your own application ID (a.k.a. client ID), all Microsoft Advertising customers can use the public "Tutorial Sample App" client ID in sandbox i.e., 4c0b021c-00c3-4508-838f-d3127e8167ff. For more information, please see Sandbox.

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.

    Note

    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.

Walkthroughs

Once you have the Bing Ads PHP SDK installed you can either browse the Bing Ads API 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."

<ns1:CriterionBid>

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

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

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:sequence>
        <xs:element minOccurs="0" name="Multiplier" type="xs:double"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
<xs:element name="BidMultiplier" nillable="true" type="tns:BidMultiplier"/>

See Also

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