This article explains how to download your Bing Ads campaigns, ad groups, keywords, and ads using the Bing Ads API Bulk Service. You can also extract this data from the Campaign Management Service, but it's much slower for retrieving more than a few items, so you'll want to use the newer Bulk Service most of the time.
The Bing Ads API Bulk Download Service
The Bulk Download service allows you to download an entire account in a single CSV file. The file is the same as the one used by the Bing Ads Desktop tool: each row contains a single entity (campaign, ad group, keyword, ad, or target) and each column contains the fields for the entity. You can find out more about the structure of the file in the Bulk Service Schema.
The Bulk Download Service works asynchronously, so to download an account you request an extract file for an account, poll the service periodically until the file is ready, and then download the file from a temporary URL provided by the API. You can then process the file and import the campaign data into your system or perform other operations. The next three sections will describe each step in detail.
LastSyncTimeInUTC: The last time you updated your system. The API will only return entities created or modified after this date. You can request everything by omitting this parameter.
Given this, you can create a download request using the following code:
This code creates a request array using a supplied $accountId variable encoded into a SOAP long array. I've chosen a tab separated file type, as it's a bit easier to parse, and the minimum required entities. To add the last update date, add a date variable and format it using as an ISO 8601 date:
Now that we've constructed a request, it can be sent to the API:
This code calls the DownloadCampaignsByAccountIds operation and extracts the resulting request id that we need for the next part of the process. The code is using a standard PHP SOAP client that we need to initialize using Bing Ads API authentication headers and the bulk service location:
This code uses the API_SERVER constant to define the API server, which is either https://api.sandbox.bingads.microsoft.com for the sandbox or https://api.bingads.microsoft.com in production, then defines the API endpoint and namespace constants and creates a new SOAP Client. Next it sets a DEVELOPER_TOKEN constant, which is always BBD37VB98 in the sandbox and your own in production, then sets the required SOAP headers. You can use this client for all subsequent requests to the API for the given $username, $password, and $accountId.
Polling the Status of the Bulk Extract
Now that we've submitted a bulk download request, we need to poll the API using the GetDownloadStatus operation until the file is ready. This operation takes the $requesrId retrieved from the DownloadCampaignsByAccountIds operation, and returns a RequestStatus and DownloadUrl. The RequestStatus indicates the processing status, which is InProgress until the request completes or fails, and the DownloadUrl indicates the location of the completed extract file ready for download. The following code polls the API until the extract is ready for download:
This script loops while the $status variable contains the status InProgress; it pauses for the number of seconds in $pollDelay, calls the GetDownloadStatus operation using the $requestId, and then extracts the resulting RequestStatus. Larger accounts take longer to process than smaller ones, so the script backs off from polling the API by increasing the $pollDelay variable each time around the loop to reduce the number of API calls it makes.
Once the extract has finished, the script can retrieve the location of the file:
Downloading the Report
Now that you've got a URL, you can use it to download your extract file using cURL:
Note that the Bing Ads API uses version 3 of SSL spec, and I've found that I had to set it explicitly as a cURL option.
The extract file is zipped up, so you need to unzip it before processing the entities and importing them into your system:
Example Bing Bulk Download Script
Here's a full script containing the steps outlined in this tutorial: