get($session, AdGroupAdService::class); $mediaService = $adWordsServices->get($session, MediaService::class); $operations = []; // Create a responsive display ad. $responsiveDisplayAd = new ResponsiveDisplayAd(); // This ad format does not allow the creation of an image using the // Image.data field. An image must first be created using the MediaService, // and Image.mediaId must be populated when creating the ad. $adImage = self::uploadImage($mediaService, 'http://goo.gl/3b9Wfh'); $marketingImage = new Image(); $marketingImage->setMediaId($adImage->getMediaId()); // var_dump($responsiveDisplayAd); $responsiveDisplayAd->setMarketingImage($marketingImage); $responsiveDisplayAd->setShortHeadline('Travel'); $responsiveDisplayAd->setLongHeadline('Travel the World'); $responsiveDisplayAd->setDescription('Take to the air!'); $responsiveDisplayAd->setBusinessName('Google'); $responsiveDisplayAd->setFinalUrls(['http://wp.pl']); // Optional: Create a square marketing image using MediaService, and set it // to the ad. $squareImage = self::uploadImage($mediaService, 'https://goo.gl/mtt54n'); $squareMarketingImage = new Image(); $squareMarketingImage->setMediaId($squareImage->getMediaId()); $responsiveDisplayAd->setSquareMarketingImage($squareMarketingImage); // Optional: Set call to action text. $responsiveDisplayAd->setCallToActionText('Shop Now'); // Optional: Set dynamic display ad settings, composed of landscape logo // image, promotion text, and price prefix. $dynamicSettings = self::createDynamicDisplayAdSettings($mediaService); $responsiveDisplayAd->setDynamicDisplayAdSettings($dynamicSettings); // Create ad group ad. $adGroupAd = new AdGroupAd(); $adGroupAd->setAdGroupId($adGroupId); $adGroupAd->setAd($responsiveSearchAd); // Optional: Set additional settings. $adGroupAd->setStatus(AdGroupAdStatus::PAUSED); // Create ad group ad operation and add it to the list. $operation = new AdGroupAdOperation(); $operation->setOperand($adGroupAd); $operation->setOperator(Operator::ADD); // Add the responsive search ad on the server. // $result = $adGroupAdService->mutate([$operation]); $operations[] = $operation; // Add a responsive display ad on the server. $result = $adGroupAdService->mutate($operations); foreach ($result->getValue() as $adGroupAd) { printf( "Responsive display ad with ID %d and short headline '%s' was added.\n", $adGroupAd->getAd()->getId(), $adGroupAd->getAd()->getShortHeadline() ); } } private static function uploadImage(MediaService $mediaService, $url) { // Creates an image and upload it to the server. $image = new Image(); $image->setData(file_get_contents($url)); $image->setType(MediaMediaType::IMAGE); return $mediaService->upload([$image])[0]; } private static function createDynamicDisplayAdSettings( MediaService $mediaService ) { $logoImage = self::uploadImage($mediaService, 'https://ibb.co/KWx5jBt'); $logo = new Image(); $logo->setMediaId($logoImage->getMediaId()); $dynamicSettings = new DynamicSettings(); $dynamicSettings->setLandscapeLogoImage($logo); $dynamicSettings->setPricePrefix('as low as'); $dynamicSettings->setPromoText('Free shipping!'); return $dynamicSettings; } public static function main() { // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct an API session configured from a properties file and the // OAuth2 credentials above. $session = (new AdWordsSessionBuilder())->fromFile() ->withOAuth2Credential($oAuth2Credential) ->build(); self::runExample( new AdWordsServices(), $session, intval(self::AD_GROUP_ID) ); } } AdResponsiveDisplayAd::main();