require_once 'vendor/autoload.php'; // Assuming you're using Composer use Google\AdsApi\Dfp\DfpServices; use Google\AdsApi\Dfp\DfpSessionBuilder; $config = [ 'applicationName' => 'YourAppName', // Your API credentials 'clientId' => '1234567890', 'clientSecret' => 'abcdefghijklmnopqrstuvwxyz', 'refreshToken' => 'zyxwvutsrqp', ]; $sessionBuilder = new DfpSessionBuilder(); $sessionBuilder->fromArray($config); $dfpServices = new DfpServices(); $dfpUser = $dfpServices->get($sessionBuilder->build());
$inventoryService = $dfpUser->getInventoryService(); $queryBuilder = $inventoryService->newStatementBuilder() ->orderBy('name ASC') ->limit(500); $adUnits = []; do { $page = $inventoryService->getAdUnitsByStatement( $queryBuilder->toStatement() ); $adUnits = array_merge($adUnits, $page->getResults()); $queryBuilder->increaseOffsetBy($queryBuilder->getLimit()); } while ($page->getTotalResultSetSize() > count($adUnits)); foreach ($adUnits as $adUnit) { echo $adUnit->getName() . "\n"; }
$inventoryService = $dfpUser->getInventoryService(); $adUnit = new Google\AdsApi\Dfp\v202105\AdUnit(); $adUnit->setName('My new ad unit'); $adUnit->setParentId(1234567890); // Replace with the ID of the parent ad unit $adUnit->setAdUnitCode('/1234567890/my-new-ad-unit'); $adUnits = $inventoryService->createAdUnits([$adUnit]); foreach ($adUnits as $adUnit) { echo $adUnit->getName() . ' created with ID ' . $adUnit->getId() . "\n"; }In this example, we're creating a new AdUnit object and setting its properties. We then call the createAdUnits method on the InventoryService to create the ad unit in our account. We're then looping through the results and printing out the name and ID of the newly created ad unit. Package library: Google Ads API PHP Client