I am using google ads API for viewing campaign data in my web application.
Here i want to dynamically assign customer id for viewing data. But in get
campaign file customer id is a constant. How can i change customer id as a
normal private variable ?
Here is the code from getCampaigns.php file
class GetCampaigns
{
private const CUSTOMER_ID = $_COOKIE['customer_id'];
//here i have tried doing this below
private $customer_id = $_COOKIE['customer_id'];
public static function main()
{
// Either pass the required parameters for this example on the
command line, or insert them
// into the constants above.
$options = (new ArgumentParser())->parseCommandArguments([
ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT
]);
// Generate a refreshable OAuth2 credential for authentication.
// $oAuth2Credential = (new
OAuth2TokenBuilder())->fromFile(__DIR__.'\google_ads_php.ini')->build();
// Construct a Google Ads client configured from a properties file
and the
// OAuth2 credentials above.
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
// We set this value to true to show how to use GAPIC v2 source
code. You can remove the
// below line if you wish to use the old-style source code.
Note that in that case, you
// probably need to modify some parts of the code below to make
it work.
// For more information, see
//
https://developers.devsite.corp.google.com/google-ads/api/docs/client-libs/php/gapic.
->usingGapicV2Source(true)
->build();
try {
$data = self::runExample(
$googleAdsClient, $this->customer_id
// $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID
// tried to chnage above line to
$options[ArgumentNames::CUSTOMER_ID] ?: $this->customer_id
);
return $data;
} 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);
}
}
}
//Updates I have made to gain result
//original line
private const CUSTOMER_ID = $_COOKIE['customer_id'];
//here i have tried doing this below
private $customer_id = $_COOKIE['customer_id'];
// original try block
try {
$data = self::runExample(
$googleAdsClient,
$options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID
);
//updated try block
try {
$data = self::runExample(
$googleAdsClient, $this->customer_id
//$options[ArgumentNames::CUSTOMER_ID] ?: $this->customer_id
);
Got an error
*Fatal error*: Uncaught Error: Using $this when not in object context in
D:\project-folder\google-ads-php\GetCampaigns.php:81 Stack trace: #0
D:\project-folder\ga-get-campaign.php(14):
Google\Ads\GoogleAds\Examples\BasicOperations\GetCampaigns::main() #1
{main} thrown in *D:\**project-folder\google-ads-php\GetCampaigns.php* on
line *81*
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/712612f4-04bc-4021-938f-2a01c9926b76n%40googlegroups.com.