I'm trying to get a list of all campaigns using the Google Ads API. To do 
this, I use an example from the google-ads-php library, but this code does 
not work for me. What's weird is that the foreach loop doesn't even run and 
I don't see the output of var_dump. Can anyone suggest me what I'm doing 
wrong? Or give a link to an example with working code?

My PHP Symfony 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);
}
}
}

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/4e21dc38-9bd1-4a23-89a6-ea384c6aa0d0n%40googlegroups.com.
  • No... Vladimir Hiuk
    • ... 'Google Ads API Forum Advisor' via Google Ads API and AdWords API Forum

Reply via email to