namespace Google\Ads\GoogleAds\Examples\BasicOperations;
require __DIR__ . '/../../vendor/autoload.php';
use GetOpt\GetOpt;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames;
use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClient;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsException;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;
use Google\Ads\GoogleAds\Lib\V3\GoogleAdsServerStreamDecorator;
use Google\Ads\GoogleAds\V3\Errors\GoogleAdsError;
use Google\Ads\GoogleAds\V3\Services\GoogleAdsRow;
use Google\Ads\GoogleAds\V3\Services\SearchGoogleAdsStreamResponse;
use Google\Ads\GoogleAds\V3\Services\SearchGoogleAdsResponse;
use Google\ApiCore\ApiException;
/** This example gets all campaigns. To add campaigns, run AddCampaigns.php. */
class GetCampaigns
{
const CUSTOMER_ID = '**********';
public static function main()
{
$options = (new ArgumentParser())->parseCommandArguments([
ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT
]);
// Generate a refreshable OAuth2 credential for authentication.
// $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
// // Construct a Google Ads client configured from a properties file and the
// // OAuth2 credentials above.
// $googleAdsClient = (new GoogleAdsClientBuilder())
// ->fromFile()
// ->withOAuth2Credential($oAuth2Credential)
// ->build();
$oAuth2Credential = (new OAuth2TokenBuilder())
->withClientId('*********-p4i6a80tjrm4kgp5akfs6bp00liq2601.apps.googleusercontent.com')
->withClientSecret("**********")
->withRefreshToken('***********************************************')
->build();
$googleAdsClient = (new GoogleAdsClientBuilder())
->withOAuth2Credential($oAuth2Credential)
->withDeveloperToken('**************')
->withLoginCustomerId('**********')
->build();
try {
self::runExample(
$googleAdsClient,
$options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID
);
} 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
);
}
} catch (ApiException $apiException) {
printf(
"ApiException was thrown with message '%s'.%s",
$apiException->getMessage(),
PHP_EOL
);
}
}
/**
* Runs the example.
*
* @param GoogleAdsClient $googleAdsClient the Google Ads API client
* @param int $customerId the customer ID
*/
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.
$stream =$googleAdsServiceClient->search($customerId, $query);
$emptyarray=array();
foreach ($stream->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
array_push($emptyarray,$googleAdsRow->getAdGroup()->getId()->getValue());
printf(
"Ad group with ID %d and name '%s' was found in campaign with ID .%s",
$googleAdsRow->getCampaign()->getId()->getValue(),
$googleAdsRow->getCampaign()->getName()->getValue(),
PHP_EOL
);
}
echo count($emptyarray);
}
}
GetCampaigns::main();Hi Sreenu,
Thanks for reaching out to us. I see that in your code you're using the get_campaigns example, but are trying to push ad_groups into your array. For reference, here is the code snippet I'm talking about.
array_push($emptyarray,$googleAdsRow->getAdGroup()->getId()->getValue());
printf( "Ad group with ID %d and name '%s' was found in campaign with ID .%s", $googleAdsRow->getCampaign()->getId()->getValue(), $googleAdsRow->getCampaign()->getName()->getValue(), PHP_EOL
Where you have "getAdGroup()->getId()->getValue()" you're trying to push into an array AdGroups when your code is querying Campaigns.
If you would like to query Ad Groups, I'd suggest using this example to obtain a list of ad groups under a campaign. Let me know if you have further questions.
Thank you,
Bryan, Google Ads API Team
Hey Bryan,
Hey Bryan,
Hey Bryan,