繁体   English   中英

为什么我在使用 google-ads-php getCampaigns 示例库时没有获得所有广告系列的列表?

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

我正在尝试使用 Google Ads API 获取所有广告系列的列表。 为此,我使用了 google-ads-php 库中的示例,但此代码不适用于我。 奇怪的是foreach循环甚至没有运行,我没有看到var_dump的output。 谁能建议我我做错了什么? 或者给出一个带有工作代码的例子的链接?

我的 PHP Symfony class 代码:

    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;
    }
}

我认为您正在尝试使用经理的 Customer_Id 获取活动列表。 如果您对经理账户进行查询,则它不会返回任何记录,因为广告系列与经理账户下的客户账户相关联。

确保使用客户端 Customer_Id 并在 header 中传递经理的 Id。

例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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