|
1 | | -# BingAds-PHP-REST-SDK |
| 1 | +# msads-rest |
| 2 | + |
| 3 | +This project provides the PHP client library for the REST-based Bing Ads API at Microsoft. |
| 4 | + |
| 5 | +For documentation visit the [Bing Ads API Getting Started Page](https://learn.microsoft.com/en-us/advertising/guides/get-started). |
| 6 | + |
| 7 | +## Installation & Usage |
| 8 | + |
| 9 | +### Requirements |
| 10 | + |
| 11 | +PHP 7.4 and later. |
| 12 | +Should also work with PHP 8.0. |
| 13 | + |
| 14 | +### Composer |
| 15 | + |
| 16 | +To install the bindings via [Composer](https://getcomposer.org/), run the following command: |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require microsoft/msads |
| 20 | +``` |
| 21 | + |
| 22 | +Then run `composer install` |
| 23 | + |
| 24 | +### Manual Installation |
| 25 | + |
| 26 | +Download the files and include `autoload.php`: |
| 27 | + |
| 28 | +```php |
| 29 | +<?php |
| 30 | +require_once('/path/to/msads-rest/vendor/autoload.php'); |
| 31 | +``` |
| 32 | + |
| 33 | +## Getting Started |
| 34 | + |
| 35 | +Please follow the [installation procedure](#installation--usage) and then run the following: |
| 36 | + |
| 37 | +```php |
| 38 | +<?php |
| 39 | +require_once(__DIR__ . '/vendor/autoload.php'); |
| 40 | + |
| 41 | +$config = \Microsoft\MsAds\Rest\Configuration::getDefaultConfiguration(); |
| 42 | + |
| 43 | +$authentication = (new \Microsoft\MsAds\Rest\Auth\OAuthDesktopMobileAuthCodeGrant()) |
| 44 | + ->withEnvironment(\Microsoft\MsAds\Rest\Auth\ApiEnvironment::SANDBOX) |
| 45 | + ->withClientId(self::CLIENT_ID) // Replace with your client ID |
| 46 | + ->withOAuthScope(\Microsoft\MsAds\Rest\Auth\OAuthScope::MSADS_MANAGE); |
| 47 | + |
| 48 | +$config->setAuthorizationData((new \Microsoft\MsAds\Rest\Auth\AuthorizationData()) |
| 49 | + ->withAuthentication($authentication) |
| 50 | + ->withDeveloperToken('BBD37VB98')); // For sandbox use 'BBD37VB98' |
| 51 | + |
| 52 | +// To set the auth key manually |
| 53 | +// $config = $config->setApiKey('Authorization', 'YOUR_API_KEY'); |
| 54 | +// $config = $config->setApiKey('UserName', 'YOUR_API_KEY'); |
| 55 | +// $config = $config->setApiKey('CustomerAccountId', 'YOUR_API_KEY'); |
| 56 | +// $config = $config->setApiKey('CustomerId', 'YOUR_API_KEY'); |
| 57 | +// $config = $config->setApiKey('DeveloperToken', 'YOUR_API_KEY'); |
| 58 | +// $config = $config->setApiKey('Password', 'YOUR_API_KEY'); |
| 59 | + |
| 60 | + |
| 61 | +$apiInstance = new \Microsoft\MsAds\Rest\Api\CampaignManagementServiceApi( |
| 62 | + // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. |
| 63 | + // This is optional, `GuzzleHttp\Client` will be used as default. |
| 64 | + new \GuzzleHttp\Client(), |
| 65 | + $config, |
| 66 | + null, |
| 67 | + \Microsoft\MsAds\Rest\Auth\ApiEnvironment::SANDBOX |
| 68 | +); |
| 69 | +$request = new \Microsoft\MsAds\Rest\Model\CampaignManagementService\AddAdExtensionsRequest(); |
| 70 | +$request->setAdExtensions([ |
| 71 | + new \Microsoft\MsAds\Rest\Model\CampaignManagementService\ImageAdExtension([ |
| 72 | + 'Name' => 'Test Image Ad Extension', |
| 73 | + ]) |
| 74 | +]); |
| 75 | + |
| 76 | +try { |
| 77 | + $result = $apiInstance->addAdExtensions($request); |
| 78 | + print_r($result); |
| 79 | +} catch (Exception $e) { |
| 80 | + echo 'Exception when calling CampaignManagementServiceApi->addAdExtensions: ', $e->getMessage(), PHP_EOL; |
| 81 | +} |
| 82 | + |
| 83 | +``` |
| 84 | + |
| 85 | +## Handling Responses and Exceptions |
| 86 | +- The API uses standard HTTP response codes to indicate the success or failure of an API request. In general, `2xx` codes indicate success, while `4xx` and `5xx` codes indicate errors. |
| 87 | +- In certain cases, the API will return an `ApplicationFault` response with an error message and an error code. The error message will provide more details about the error. |
| 88 | +- If there is an error with the API call itself, the SDK will throw an `ApiException` with the error message and code. You can catch this exception and handle it accordingly. |
| 89 | + |
| 90 | +Example of handling responses: |
| 91 | +```php |
| 92 | +try { |
| 93 | + $response = $apiInstance->addAdExtensions($AddAdExtensionsRequest); |
| 94 | + if ($response instanceof ApplicationFault) { |
| 95 | + echo 'Handle the ApplicationFault response here'; |
| 96 | + } else { |
| 97 | + echo 'Handle the AddAdExtensionsResponse response here'; |
| 98 | + } |
| 99 | +} catch (ApiException $e) { |
| 100 | + echo 'Exception when calling AdExtensionService->addAdExtensions: ', $e->getMessage(), PHP_EOL; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Flag Enums |
| 105 | +The SDK uses `Flag Enums` to represent various options and settings. |
| 106 | +- These `Flag Enums` allow you to combine multiple options. For example, you can use the `CampaignManagementService\AdExtensionsTypeFilter` enum to specify multiple AdExtensionTypeFilters using an array of values OR comma-separated string. |
| 107 | +- To check if an Enum class is a `Flag Enum`, you can look for the `$isFlags` property in the class definition. If it is set to `true`, you can use the enum as a `Flag Enum`. |
| 108 | +- When a `Flag Enum` is returned from the API in deserialization, it will be represented as a string with the values separated by commas. |
| 109 | + |
| 110 | +```php |
| 111 | +use \Microsoft\MsAds\Rest\Model\CampaignManagementService\AdExtensionsTypeFilter; |
| 112 | +// To represent multiple AdExtensionTypeFilters: |
| 113 | +$filter = [AdExtensionsTypeFilter::ACTION_AD_EXTENSION, AdExtensionsTypeFilter::APP_AD_EXTENSION]; |
| 114 | +// or |
| 115 | +$filter = 'ActionAdExtension,AppAdExtension'; |
| 116 | +// or |
| 117 | +$filter = AdExtensionsTypeFilter::ACTION_AD_EXTENSION . ',' . AdExtensionsTypeFilter::APP_AD_EXTENSION; |
| 118 | +// or |
| 119 | +$filter = new AdExtensionsTypeFilter($filter); |
| 120 | +``` |
| 121 | + |
| 122 | +## Authorization |
| 123 | +You can use the OAuth 2.0 authorization flow to obtain the necessary tokens for accessing the Bing Ads API. The SDK supports both web-based and desktop-based OAuth flows. |
| 124 | +You can find more information on how to obtain these tokens in the [Bing Ads API documentation](https://learn.microsoft.com/en-us/advertising/guides/authentication-oauth). |
| 125 | +- Classes are provided in the SDK to help you with the OAuth 2.0 flow, within the folder [src/Auth](src/Auth). |
| 126 | +- Sample code can be found in the `test` directory, specifically in the [test/WebAuthentication](test/WebAuthentication) directory for webpage auth, and [test/RestApiTestBase.php](test/RestApiTestBase.php) for OAuth Desktop auth. |
| 127 | + |
| 128 | +Authentication schemes defined for the API: |
| 129 | +### Authorization |
| 130 | +- **Type**: API key |
| 131 | +- **API key parameter name**: Authorization |
| 132 | +- **Location**: HTTP header |
| 133 | + |
| 134 | +### UserName |
| 135 | +- **Type**: API key |
| 136 | +- **API key parameter name**: UserName |
| 137 | +- **Location**: HTTP header |
| 138 | + |
| 139 | +### Password |
| 140 | +- **Type**: API key |
| 141 | +- **API key parameter name**: Password |
| 142 | +- **Location**: HTTP header |
| 143 | + |
| 144 | +### CustomerAccountId |
| 145 | +- **Type**: API key |
| 146 | +- **API key parameter name**: CustomerAccountId |
| 147 | +- **Location**: HTTP header |
| 148 | + |
| 149 | +### CustomerId |
| 150 | +- **Type**: API key |
| 151 | +- **API key parameter name**: CustomerId |
| 152 | +- **Location**: HTTP header |
| 153 | + |
| 154 | +### DeveloperToken |
| 155 | +- **Type**: API key |
| 156 | +- **API key parameter name**: DeveloperToken |
| 157 | +- **Location**: HTTP header |
| 158 | + |
| 159 | +## Samples and Tests |
| 160 | + |
| 161 | +There are some samples included in the [test](test) directory showing examples of how to use the SDK. |
| 162 | + |
| 163 | +The samples are written using PHPUnit. To run the tests, you need to have PHPUnit installed. You can install it globally or use Composer to install it as a dependency. |
| 164 | + |
| 165 | +> **Warning**: Before running the tests, make sure to set up your environment variables for the API keys. You can do this by editing the CONSTANTS in the [RestApiTestBase.php](test/RestApiTestBase.php) file, going through the auth flow to get the `CODE_URL` and then replacing that constant. |
| 166 | +
|
| 167 | +To run the tests, use an IDE or run the following command in the terminal: |
| 168 | + |
| 169 | +```bash |
| 170 | +composer install |
| 171 | +vendor/bin/phpunit |
| 172 | +``` |
| 173 | + |
| 174 | +Logs will be generated in the base directory of the project with the request and response data for further analysis. |
| 175 | + |
| 176 | +## About this package |
| 177 | + |
| 178 | +- SDK Package version: `13.0.24.1` |
| 179 | +- Generated date: `2025-06-11T22:35:13.192919100-04:00[America/New_York]` |
0 commit comments