Why am I not getting a list of all campaigns when using the google-ads-php getCampaigns example library?

71 views
Skip to first unread message

Vladimir Hiuk

unread,
Mar 9, 2022, 5:49:46 AM3/9/22
to Google Ads API and AdWords API Forum
As a result of executing the code below, I do not receive any data. Although, for example, such an example as GetAccountHierarchy works for me. I am currently working on migrating a running project from adWords API to Ads API. Can it be not in the code, but in the account settings?

My PHP Class Code:

class CheckController extends AbstractController
{

public function index()
{
$config = $this->getParameter('kernel.project_dir') . '/google_ads_php.ini';

if (!is_file($config)) return $this->json([$config]);

$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile()
->build();
//dd($oAuth2Credential);
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();

try {
self::runExample(
$googleAdsClient,
xxxxxxxxxx
);
} catch (GoogleAdsException $googleAdsException) {
printf(
"Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
$googleAdsException->getRequestId(),
PHP_EOL,
PHP_EOL
);
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
/** @var GoogleAdsError $error */
printf(
"\t%s: %s%s",
$error->getErrorCode()->getErrorCode(),
$error->getMessage(),
PHP_EOL
);
}
exit(1);
} catch (ApiException $apiException) {
printf(
"ApiException was thrown with message '%s'.%s",
$apiException->getMessage(),
PHP_EOL
);
exit(1);
}
}

/**
* @param GoogleAdsClient $googleAdsClient
* @param int $customerId
* @return void
* @throws ApiException
*/
public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId)
{
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
// Creates a query that retrieves all campaigns.
$query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
// Issues a search stream request.
/** @var GoogleAdsServerStreamDecorator $stream */
$stream =
$googleAdsServiceClient->searchStream($customerId, $query);

// Iterates over all rows in all messages and prints the requested field values for
// the campaign in each row.
foreach ($stream->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
printf(
"Campaign with ID %d and name '%s' was found.%s",
$googleAdsRow->getCampaign()->getId(),
$googleAdsRow->getCampaign()->getName(),
PHP_EOL
);
}
}

public static function runListExample(GoogleAdsClient $googleAdsClient)
{
$customerServiceClient = $googleAdsClient->getCustomerServiceClient();

// Issues a request for listing all accessible customers.
$accessibleCustomers = $customerServiceClient->listAccessibleCustomers();
print 'Total results: ' . count($accessibleCustomers->getResourceNames()) . PHP_EOL;

// Iterates over all accessible customers' resource names and prints them.
foreach ($accessibleCustomers->getResourceNames() as $resourceName) {
/** @var string $resourceName */
printf("Customer resource name: '%s'%s", $resourceName, PHP_EOL);
}
}
}
Reply all
Reply to author
Forward
0 new messages