简体   繁体   中英

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

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
{
    /**
     * @Route("/check", name="check")
     */
    public function index(): Response
    {
        $config = $this->getParameter('kernel.project_dir') . '/google_ads_php.ini';

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

        $oAuth2Credential = (new OAuth2TokenBuilder())
            ->fromFile($config)
            ->build();

        $googleAdsClient = (new GoogleAdsClientBuilder())
            ->fromFile($config)
            ->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);
        }

        $test = self::runExample($googleAdsClient, '1138211281');

        foreach ($test->iterateAllElements() as $googleAdsRow) {
            echo '<pre>';
                var_dump($googleAdsRow->getCampaign()->getId());
            echo '</pre>';
        }

        /*echo '<pre>';
                var_dump($test);
        echo '</pre>';*/

        foreach ($test->iterateAllElements() as $googleAdsRow) {
            /** @var GoogleAdsRow $googleAdsRow */
            var_dump($googleAdsRow->getCampaign()->getId());
        }

        /*return $this->render('base.html.twig', [
            'test' => $test
        ]);*/

         return $this->json([
             'message' => 'Welcome to your new controller!',
             'path' => 'src/Controller/CheckController.php',
         ]);
    }

    /**
     * @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
            );
        }
        return $stream;
    }
}

I am not familiar with this but I hope this link helps a little https://developers.google.com/google-ads/api/docs/samples/get-campaigns#php

I think you are trying to get a list of campaigns using the manager's Customer_Id. If you make a query to the manager account then it will return no records as the campaigns are associated with the client accounts under the manager account.

Make sure you use client Customer_Id and pass manager's Id in the header.

Example:

$googleAdsClient = (new GoogleAdsClientBuilder())
        ->fromFile()
        ->withOAuth2Credential($oAuth2Credential)
        ->withLoginCustomerId('123456789')
        ->build();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM