Closed sreenumalae closed 4 years ago
So do you get an error when trying to run GetCampaigns.php on your server? Does the error go away when you run GetAdGroups.php instead?
Nope In GetAdGroups im not getting any error Im getting a blank page
Do you mean your specified campaign doesn't contain any ad groups? And what errors in detail you got from running GetCampaigns?
No i have groups but wen i load the adgroups in the server i'm not getting any errors and and also no output. But if i run the GetCampaign im getting error
What errors did you get?
And please note that code examples are meant to be run from command prompt, not via the web browsers. So, if you run them directly on web browsers, you won't see anything.
Then how can i do this in web-application. I mean is there any examples?
We don't have any examples to do that in web application, mainly because there are many web application frameworks and each can require different mechanisms to implement. We did provide an example for Laravel in our old library though.
Please let me know if you have other questions.,
Hey @fiboknacky , Thanks for your reply. In AdGroups i'm getting output without using search stream. But in getcampaigns im getting null values
Fatal error: Uncaught Error: Call to a member function iterateAllElements() on null in C:\xampp\htdocs\google-ads-php\examples\BasicOperations\GetCampaigns.php:117 Stack trace:
thrown in C:\xampp\htdocs\google-ads-php\examples\BasicOperations\GetCampaigns.php on line 117
Can you attach the code you're using?
<?php
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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()
{
// 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())
->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.
/** @var GoogleAdsServerStreamDecorator $stream */
$stream =$googleAdsServiceClient->search($customerId, $query);
foreach ($response->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
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
);
}
}
}
GetCampaigns::main();
Your code has a bug:
stream =$googleAdsServiceClient->search($customerId, $query);
foreach ($response->iterateAllElements() as $googleAdsRow
You created a $stream
variable, but refer to $response
. $response
is thus null.
Hey @fiboknacky I copied the code from wrong place But I even cross checked my code
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->search($customerId, $query);
foreach ($stream->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
printf(
"Campaign with ID %d and name '%s' was found.%s",
$googleAdsRow->getCampaign()->getId()->getValue(),
$googleAdsRow->getCampaign()->getName()->getValue(),
PHP_EOL
);
}
}
Still im getting null Just to verify i added the array also. In the echo count(emptyarray), I'm getting 0 as output
Tips for GitHub: Please use three backticks like this ```php
And put them in the different lines than your code. This will make your code easier to read. I've just updated your reply for reference.
Just to verify i added the array also. In the echo count(emptyarray), I'm getting 0 as output
I cannot find that in your code. Can you update your code?
Hey @fiboknacky thanks for the tip.
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();
Your request fetches only campaigns, but your array tries to add ad groups. That's why there are no values. If you need more help on the Google Ads API queries, please contact this forum.
Closing as the solution is provided.
Hey @fiboknacky As we discussed in the previous issue https://github.com/googleads/google-ads-php/issues/304 The Grpc is recommended or it really needed? As i already mentioned that i cannot add grpc to my php. So, is there any way to get through this? If possible please provide some example.