|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Microsoft\BingAds\Samples\V13; |
| 4 | + |
| 5 | +// For more information about installing and using the Bing Ads PHP SDK, |
| 6 | +// see https://go.microsoft.com/fwlink/?linkid=838593. |
| 7 | + |
| 8 | +//require_once __DIR__ . "/../vendor/autoload.php"; |
| 9 | + |
| 10 | +include __DIR__ . "/AuthHelper.php"; |
| 11 | +include __DIR__ . "/CampaignManagementExampleHelper.php"; |
| 12 | + |
| 13 | +use SoapVar; |
| 14 | +use SoapFault; |
| 15 | +use Exception; |
| 16 | + |
| 17 | +//Specify the Microsoft\BingAds\V13\CampaignManagement classes that will be used. |
| 18 | +use Microsoft\BingAds\V13\CampaignManagement\CallToAction; |
| 19 | +use Microsoft\BingAds\V13\CampaignManagement\LanguageName; |
| 20 | +use Microsoft\BingAds\V13\CampaignManagement\Campaign; |
| 21 | +use Microsoft\BingAds\V13\CampaignManagement\CampaignType; |
| 22 | +use Microsoft\BingAds\V13\CampaignManagement\BudgetLimitType; |
| 23 | +use Microsoft\BingAds\V13\CampaignManagement\AdGroup; |
| 24 | +use Microsoft\BingAds\V13\CampaignManagement\Bid; |
| 25 | +use Microsoft\BingAds\V13\CampaignManagement\Date; |
| 26 | +use Microsoft\BingAds\V13\CampaignManagement\ResponsiveAd; |
| 27 | +use Microsoft\BingAds\V13\CampaignManagement\ImageAsset; |
| 28 | +use Microsoft\BingAds\V13\CampaignManagement\TextAsset; |
| 29 | + |
| 30 | +// Specify the Microsoft\BingAds\Auth classes that will be used. |
| 31 | +use Microsoft\BingAds\Auth\ServiceClient; |
| 32 | +use Microsoft\BingAds\Auth\ServiceClientType; |
| 33 | + |
| 34 | +// Specify the Microsoft\BingAds\Samples classes that will be used. |
| 35 | +use Microsoft\BingAds\Samples\V13\AuthHelper; |
| 36 | +use Microsoft\BingAds\Samples\V13\CampaignManagementExampleHelper; |
| 37 | + |
| 38 | +try |
| 39 | +{ |
| 40 | + // Authenticate user credentials and set the account ID for the sample. |
| 41 | + AuthHelper::Authenticate(); |
| 42 | + |
| 43 | + // To run this example you'll need to provide a valid Ad Final URL |
| 44 | + $adFinalUrl = "https://contoso.com"; |
| 45 | + // Set false to disable cleanup of created entities at the end |
| 46 | + $doCleanup = true; |
| 47 | + |
| 48 | + print("-----\r\nCreateResponsiveAdRecommendation:\r\n"); |
| 49 | + printf("-----\r\nGetting ad recommendation for URL %s ...\r\n", $adFinalUrl); |
| 50 | + $finalUrls = array($adFinalUrl); |
| 51 | + $responsiveAdRecommendationResponse = CampaignManagementExampleHelper::CreateResponsiveAdRecommendation( |
| 52 | + $finalUrls |
| 53 | + ); |
| 54 | + $responsiveAd = $responsiveAdRecommendationResponse->ResponsiveAd; |
| 55 | + $newResponsiveAd = new ResponsiveAd(); |
| 56 | + $newResponsiveAd->DevicePreference = $responsiveAd->DevicePreference; |
| 57 | + $newResponsiveAd->FinalUrls = $responsiveAd->FinalUrls; |
| 58 | + $newResponsiveAd->ForwardCompatibilityMap = $responsiveAd->ForwardCompatibilityMap; |
| 59 | + $newResponsiveAd->Headlines = $responsiveAd->Headlines; |
| 60 | + $newResponsiveAd->LongHeadlines = $responsiveAd->LongHeadlines; |
| 61 | + $newResponsiveAd->Descriptions = $responsiveAd->Descriptions; |
| 62 | + $newResponsiveAd->CallToAction = $responsiveAd->CallToAction; |
| 63 | + copyAssetLinks($newResponsiveAd->Headlines); |
| 64 | + copyAssetLinks($newResponsiveAd->LongHeadlines); |
| 65 | + copyAssetLinks($newResponsiveAd->Descriptions); |
| 66 | + $imageSuggestions = $responsiveAdRecommendationResponse->ImageSuggestions->AdRecommendationImageSuggestion; |
| 67 | + |
| 68 | + // Select a few images from the suggested list. This example picks first 5 images |
| 69 | + $selectedImages = array_slice($imageSuggestions, 0, 5); |
| 70 | + |
| 71 | + // Add selected images to your media library |
| 72 | + saveImages($selectedImages); |
| 73 | + |
| 74 | + $images = array_map(function($x) { |
| 75 | + return $x->AssetLink; |
| 76 | + }, $selectedImages); |
| 77 | + $newResponsiveAd->Images = $images; |
| 78 | + |
| 79 | + $newResponsiveAd->BusinessName = "Contoso"; |
| 80 | + $newResponsiveAd->CallToActionLanguage = LanguageName::English; |
| 81 | + |
| 82 | + // Create an Audience campaign with one ad group and a responsive ad |
| 83 | + $campaigns = array(); |
| 84 | + $campaign = new Campaign(); |
| 85 | + // CampaignType must be set for Audience campaigns |
| 86 | + $campaign->CampaignType = CampaignType::Audience; |
| 87 | + $campaign->Name = "Ad recommendation test " . $_SERVER['REQUEST_TIME']; |
| 88 | + $campaign->BudgetType = BudgetLimitType::DailyBudgetStandard; |
| 89 | + $campaign->DailyBudget = 50.00; |
| 90 | + // Languages must be set for Audience campaigns |
| 91 | + $campaign->Languages = array("All"); |
| 92 | + $campaign->TimeZone = "PacificTimeUSCanadaTijuana"; |
| 93 | + $campaigns[] = $campaign; |
| 94 | + |
| 95 | + print("-----\r\nAddCampaigns:\r\n"); |
| 96 | + $addCampaignsResponse = CampaignManagementExampleHelper::AddCampaigns( |
| 97 | + $GLOBALS['AuthorizationData']->AccountId, |
| 98 | + $campaigns |
| 99 | + ); |
| 100 | + $campaignIds = $addCampaignsResponse->CampaignIds; |
| 101 | + print("CampaignIds:\r\n"); |
| 102 | + CampaignManagementExampleHelper::OutputArrayOfLong($campaignIds); |
| 103 | + print("PartialErrors:\r\n"); |
| 104 | + CampaignManagementExampleHelper::OutputArrayOfBatchError($addCampaignsResponse->PartialErrors); |
| 105 | + |
| 106 | + // Add an ad group within the campaign. |
| 107 | + $adGroups = array(); |
| 108 | + $adGroup = new AdGroup(); |
| 109 | + $adGroup->CpcBid = new Bid(); |
| 110 | + $adGroup->CpcBid->Amount = 0.09; |
| 111 | + date_default_timezone_set('UTC'); |
| 112 | + $endDate = new Date(); |
| 113 | + $endDate->Day = 31; |
| 114 | + $endDate->Month = 12; |
| 115 | + $endDate->Year = date("Y"); |
| 116 | + $adGroup->EndDate = $endDate; |
| 117 | + $adGroup->Name = "Holiday Sale"; |
| 118 | + $adGroup->StartDate = null; |
| 119 | + // Network cannot be set for ad groups in Audience campaigns |
| 120 | + $adGroup->Network = null; |
| 121 | + $adGroups[] = $adGroup; |
| 122 | + |
| 123 | + print("-----\r\nAddAdGroups:\r\n"); |
| 124 | + $addAdGroupsResponse = CampaignManagementExampleHelper::AddAdGroups( |
| 125 | + $campaignIds->long[0], |
| 126 | + $adGroups, |
| 127 | + null |
| 128 | + ); |
| 129 | + $adGroupIds = $addAdGroupsResponse->AdGroupIds; |
| 130 | + print("AdGroupIds:\r\n"); |
| 131 | + CampaignManagementExampleHelper::OutputArrayOfLong($adGroupIds); |
| 132 | + print("PartialErrors:\r\n"); |
| 133 | + CampaignManagementExampleHelper::OutputArrayOfBatchError($addAdGroupsResponse->PartialErrors); |
| 134 | + |
| 135 | + // Add a responsive ad within the ad group. |
| 136 | + $ads = array(); |
| 137 | + $ads[] = new SoapVar( |
| 138 | + $newResponsiveAd, |
| 139 | + SOAP_ENC_OBJECT, |
| 140 | + 'ResponsiveAd', |
| 141 | + $GLOBALS['CampaignManagementProxy']->GetNamespace() |
| 142 | + ); |
| 143 | + |
| 144 | + print("-----\r\nAddAds:\r\n"); |
| 145 | + $addAdsResponse = CampaignManagementExampleHelper::AddAds( |
| 146 | + $adGroupIds->long[0], |
| 147 | + $ads |
| 148 | + ); |
| 149 | + print("AdIds:\r\n"); |
| 150 | + CampaignManagementExampleHelper::OutputArrayOfLong($addAdsResponse->AdIds); |
| 151 | + print("PartialErrors:\r\n"); |
| 152 | + CampaignManagementExampleHelper::OutputArrayOfBatchError($addAdsResponse->PartialErrors); |
| 153 | + |
| 154 | + if (!$doCleanup) { |
| 155 | + return; |
| 156 | + } |
| 157 | + else { |
| 158 | + // Delete the account's media. |
| 159 | + print("-----\r\nDeleteMedia:\r\n"); |
| 160 | + $mediaIds = array_map(function($x) { |
| 161 | + return $x->Asset->enc_value->Id; |
| 162 | + }, $newResponsiveAd->Images); |
| 163 | + CampaignManagementExampleHelper::DeleteMedia( |
| 164 | + $GLOBALS['AuthorizationData']->AccountId, |
| 165 | + $mediaIds |
| 166 | + ); |
| 167 | + |
| 168 | + foreach ($mediaIds as $id) |
| 169 | + { |
| 170 | + printf("Deleted Media Id %s\r\n", $id); |
| 171 | + } |
| 172 | + |
| 173 | + // Delete the campaign and everything it contains e.g., ad groups and ads. |
| 174 | + |
| 175 | + print("-----\r\nDeleteCampaigns:\r\n"); |
| 176 | + CampaignManagementExampleHelper::DeleteCampaigns( |
| 177 | + $GLOBALS['AuthorizationData']->AccountId, |
| 178 | + array($campaignIds->long[0]) |
| 179 | + ); |
| 180 | + printf("Deleted CampaignId %s\r\n", $campaignIds->long[0]); |
| 181 | + } |
| 182 | +} |
| 183 | +catch (SoapFault $e) |
| 184 | +{ |
| 185 | + printf("-----\r\nFault Code: %s\r\nFault String: %s\r\nFault Detail: \r\n", $e->faultcode, $e->faultstring); |
| 186 | + var_dump($e->detail); |
| 187 | + print "-----\r\nLast SOAP request/response:\r\n"; |
| 188 | + print $GLOBALS['Proxy']->GetWsdl() . "\r\n"; |
| 189 | + print $GLOBALS['Proxy']->GetService()->__getLastRequest()."\r\n"; |
| 190 | + print $GLOBALS['Proxy']->GetService()->__getLastResponse()."\r\n"; |
| 191 | +} |
| 192 | +catch (Exception $e) |
| 193 | +{ |
| 194 | + // Ignore fault exceptions that we already caught. |
| 195 | + if ($e->getPrevious()) |
| 196 | + { ; } |
| 197 | + else |
| 198 | + { |
| 199 | + print $e->getCode()." ".$e->getMessage()."\r\n"; |
| 200 | + print $e->getTraceAsString()."\r\n"; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +function copyAssetLinks($assetLinks) { |
| 205 | + for ($i = 0; $i < count($assetLinks->AssetLink); $i++) { |
| 206 | + $textAsset = new TextAsset(); |
| 207 | + foreach ($assetLinks->AssetLink[$i]->Asset as $key => $value) { |
| 208 | + $textAsset->$key = $value; |
| 209 | + } |
| 210 | + $assetLinks->AssetLink[$i]->Asset = new SoapVar( |
| 211 | + $textAsset, |
| 212 | + SOAP_ENC_OBJECT, |
| 213 | + 'TextAsset', |
| 214 | + $GLOBALS['CampaignManagementProxy']->GetNamespace() |
| 215 | + ); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +function saveImages($imageSuggestions) { |
| 220 | + $mediaToAdd = array(); |
| 221 | + |
| 222 | + foreach ($imageSuggestions as $imageSuggestion) { |
| 223 | + $image = $imageSuggestion->Image; |
| 224 | + |
| 225 | + $imageBytes = downloadBytes($imageSuggestion->ImageUrl); |
| 226 | + $imageBase64 = base64_encode($imageBytes); |
| 227 | + |
| 228 | + $image->Data = $imageBase64; |
| 229 | + |
| 230 | + $encodedImage = new SoapVar( |
| 231 | + $image, |
| 232 | + SOAP_ENC_OBJECT, |
| 233 | + 'Image', |
| 234 | + $GLOBALS['CampaignManagementProxy']->GetNamespace() |
| 235 | + ); |
| 236 | + |
| 237 | + $mediaToAdd[] = $encodedImage; |
| 238 | + } |
| 239 | + |
| 240 | + $addMediaResponse = CampaignManagementExampleHelper::AddMedia( |
| 241 | + $GLOBALS['AuthorizationData']->AccountId, |
| 242 | + $mediaToAdd |
| 243 | + ); |
| 244 | + $mediaIds = $addMediaResponse->MediaIds; |
| 245 | + |
| 246 | + for ($i = 0; $i < count($mediaIds->long); $i++) { |
| 247 | + $imageSuggestions[$i]->AssetLink->Asset->Id = $mediaIds->long[$i]; |
| 248 | + $imageAsset = new ImageAsset(); |
| 249 | + foreach ($imageSuggestions[$i]->AssetLink->Asset as $key => $value) { |
| 250 | + $imageAsset->$key = $value; |
| 251 | + } |
| 252 | + $imageSuggestions[$i]->AssetLink->Asset = new SoapVar( |
| 253 | + $imageAsset, |
| 254 | + SOAP_ENC_OBJECT, |
| 255 | + 'ImageAsset', |
| 256 | + $GLOBALS['CampaignManagementProxy']->GetNamespace() |
| 257 | + ); |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +function downloadBytes($url) { |
| 262 | + $outputStream = fopen('php://temp', 'r+'); |
| 263 | + $buffer = ''; |
| 264 | + $bufferSize = 4096; |
| 265 | + |
| 266 | + $inputStream = fopen($url, 'rb'); |
| 267 | + if ($inputStream === false) { |
| 268 | + throw new Exception("Unable to open URL: $url"); |
| 269 | + } |
| 270 | + |
| 271 | + try { |
| 272 | + while (!feof($inputStream)) { |
| 273 | + $buffer = fread($inputStream, $bufferSize); |
| 274 | + if ($buffer === false) { |
| 275 | + throw new Exception("Error reading from URL: $url"); |
| 276 | + } |
| 277 | + fwrite($outputStream, $buffer); |
| 278 | + } |
| 279 | + |
| 280 | + rewind($outputStream); |
| 281 | + $result = stream_get_contents($outputStream); |
| 282 | + if ($result === false) { |
| 283 | + throw new Exception("Error reading from temporary stream"); |
| 284 | + } |
| 285 | + |
| 286 | + return $result; |
| 287 | + } finally { |
| 288 | + fclose($inputStream); |
| 289 | + fclose($outputStream); |
| 290 | + } |
| 291 | +} |
0 commit comments