googleads / google-ads-php

Google Ads API Client Library for PHP
https://developers.google.com/google-ads/api/docs/client-libs/php
Apache License 2.0
295 stars 262 forks source link

Switching from V13 to V15 & getting error while uploading click conversion #973

Closed AlanTY125 closed 8 months ago

AlanTY125 commented 10 months ago

Hi,

I am using PHP version 8.1 and PHP client library for the Google Ads API "googleads/google-ads-php": "^21.1". Used the following code to create an instance of GoogleAdsClient Object

$oAuth2Credential = (new OAuth2TokenBuilder()) ->withClientId($this->app_client_id) ->withClientSecret($this->app_client_secret) ->withRefreshToken($this->refreshtoken) ->build(); $googleAdsClient = (new GoogleAdsClientBuilder()) ->withDeveloperToken($this->developerToken) ->withOAuth2Credential($oAuth2Credential) ->withLoginCustomerId($this->customerId) ->usingGapicV2Source(true) ->build();

After that, i used the code in the following URL to create a conversion action

https://developers.google.com/google-ads/api/docs/conversions/upload-clicks

conversion action was created successfully, after I used the code on the same page to insert a conversion into that conversion action, and I'm facing the following error

TypeError: Google\ApiCore\Middleware\RetryMiddleware::Google\ApiCore\Middleware{closure}(): Argument #1 ($e) must be of type Exception, Error given, called in /var/www/html/v5projects/googleads2024/vendor/google/gax/src/Middleware/RetryMiddleware.php on line 109 in file /var/www/html/v5projects/googleads2024/vendor/google/gax/src/Middleware/RetryMiddleware.php on line 183

fiboknacky commented 10 months ago

Could you update all dependencies and try again? If the issue persists, could you share the versions of other dependencies, such as gax and protobuf?

AlanTY125 commented 10 months ago

Updated the dependencies and still the same error gax version: 1.25.0 google/protobuf : v3.25.1

fiboknacky commented 10 months ago

It looks like this is an issue on gax-php. I'd need more information to help the owner of that library fix this.

Could you please share your request and response logs by posting on the Google Ads API forum? Let the agents there know that you want to share the request and response logs privately to "the PHP client library owner". They'll provide you instructions and pass the data to me.

psociety commented 10 months ago

I can replicate the error with:


use Google\Ads\GoogleAds\Lib\V15\GoogleAdsClient;
use Google\Ads\GoogleAds\Util\V15\ResourceNames;
use Google\Ads\GoogleAds\V15\Services\KeywordAndUrlSeed;
use Google\Ads\GoogleAds\V15\Services\KeywordSeed;
use Google\Ads\GoogleAds\V15\Services\UrlSeed;
use Google\Ads\GoogleAds\V15\Enums\KeywordPlanNetworkEnum\KeywordPlanNetwork;

function generateKeywords(
        array $locationIds,
        int $languageId,
        array $keywords,
        ?string $pageUrl = null
    ) {
        $keywordPlanIdeaServiceClient = $this->googleAdsClient->getKeywordPlanIdeaServiceClient();

        // Make sure that keywords and/or page URL were specified. The request must have exactly one
        // of urlSeed, keywordSeed, or keywordAndUrlSeed set.
        if (empty($keywords) && is_null($pageUrl)) {
            throw new \InvalidArgumentException(
                'At least one of keywords or page URL is required, but neither was specified.'
            );
        }

        // Specify the optional arguments of the request as a keywordSeed, urlSeed,
        // or keywordAndUrlSeed.
        $requestOptionalArgs = [];
        if (empty($keywords)) {
            // Only page URL was specified, so use a UrlSeed.
            $requestOptionalArgs['urlSeed'] = new UrlSeed(['url' => $pageUrl]);
        } elseif (is_null($pageUrl)) {
            // Only keywords were specified, so use a KeywordSeed.
            $requestOptionalArgs['keywordSeed'] = new KeywordSeed(['keywords' => $keywords]);
        } else {
            // Both page URL and keywords were specified, so use a KeywordAndUrlSeed.
            $requestOptionalArgs['keywordAndUrlSeed'] =
                new KeywordAndUrlSeed(['url' => $pageUrl, 'keywords' => $keywords]);
        }

        // Create a list of geo target constants based on the resource name of specified location
        // IDs.
        $geoTargetConstants = array_map(function ($locationId) {
            return ResourceNames::forGeoTargetConstant($locationId);
        }, $locationIds);

        // Generate keyword ideas based on the specified parameters.
        $response = $keywordPlanIdeaServiceClient->generateKeywordIdeas(
            [
                // Set the language resource using the provided language ID.
                'language' => ResourceNames::forLanguageConstant($languageId),
                'customerId' => self::CUSTOMER_ID,
                // Add the resource name of each location ID to the request.
                'geoTargetConstants' => $geoTargetConstants,
                // Set the network. To restrict to only Google Search, change the parameter below to
                // KeywordPlanNetwork::GOOGLE_SEARCH.
                'keywordPlanNetwork' => KeywordPlanNetwork::GOOGLE_SEARCH_AND_PARTNERS
            ] + $requestOptionalArgs
        );

        $list = [];

        // Iterate over the results and print its detail.
        foreach ($response->iterateAllElements() as $result) {
            $list[] = $result->getText();
            /*
            printf(
                "Keyword idea text '%s' has %d average monthly searches and competition as %d.%s",
                $result->getText(),
                is_null($result->getKeywordIdeaMetrics()) ?
                    0 : $result->getKeywordIdeaMetrics()->getAvgMonthlySearches(),
                is_null($result->getKeywordIdeaMetrics()) ?
                    0 : $result->getKeywordIdeaMetrics()->getCompetition(),
                PHP_EOL
            );*/
        }

        return $list;
    }

$list = generateKeywords([2250], 1002, ["test"]);

the function is copied from the sample on the doc.

        "php": "^8.2",
        "google/apiclient": "^2.15",
        "googleads/google-ads-php": "^21.1",
        "guzzlehttp/guzzle": "^7.2",

If i dig into the code and print $e->getMessage() i get: Call to undefined function Google\Protobuf\Internal\bccomp()

After installing bmatch php ext i stopped getting the error.

fiboknacky commented 10 months ago

Hello @psociety,

Thanks for more information.

Call to undefined function Google\Protobuf\Internal\bccomp()

At what line of what repository (this library or gax-php) do you see this error message?

AlanTY125 commented 10 months ago

Error I am getting is

Fatal error: Uncaught TypeError: Google\ApiCore\Middleware\RetryMiddleware::Google\ApiCore\Middleware{closure}(): Argument #1 ($e) must be of type Exception, Error given, called in /var/www/html/v5projects/myproject/vendor/google/gax/src/Middleware/RetryMiddleware.php on line 109 and defined in /var/www/html/v5projects/myproject/vendor/google/gax/src/Middleware/RetryMiddleware.php:183 Stack trace: #0 /var/www/html/v5projects/myproject/vendor/google/gax/src/Middleware/RetryMiddleware.php(109): Google\ApiCore\Middleware\RetryMiddleware->Google\ApiCore\Middleware{closure}() #1 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/Promise.php(209): Google\ApiCore\Middleware\RetryMiddleware->Google\ApiCore\Middleware{closure}() #2 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/Promise.php(158): GuzzleHttp\Promise\Promise::callHandler() #3 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/TaskQueue.php(52): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise{closure}() #4 /var/www/html/v5projects/myproject/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(163): GuzzleHttp\Promise\TaskQueue->run() #5 /var/www/html/v5projects/myproject/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(189): GuzzleHttp\Handler\CurlMultiHandler->tick() #6 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/Promise.php(251): GuzzleHttp\Handler\CurlMultiHandler->execute() #7 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/Promise.php(227): GuzzleHttp\Promise\Promise->invokeWaitFn() #8 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/Promise.php(272): GuzzleHttp\Promise\Promise->waitIfPending() #9 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/Promise.php(229): GuzzleHttp\Promise\Promise->invokeWaitList() #10 /var/www/html/v5projects/myproject/vendor/guzzlehttp/promises/src/Promise.php(69): GuzzleHttp\Promise\Promise->waitIfPending() #11 /var/www/html/v5projects/myproject/vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/V15/Services/Client/ConversionUploadServiceClient.php(285): GuzzleHttp\Promise\Promise->wait() #12 /var/www/html/v5projects/myproject/UploadOfflineConversion.php(261): Google\Ads\GoogleAds\V15\Services\Client\ConversionUploadServiceClient->uploadClickConversions() #13 /var/www/html/v5projects/myproject/UploadOfflineConversion.php(136): UploadOfflineConversion\UploadOfflineConversion::runExample() #14 /var/www/html/v5projects/myproject/UploadOfflineConversion.php(289): UploadOfflineConversion\UploadOfflineConversion::main() #15 {main} thrown in /var/www/html/v5projects/myproject/vendor/google/gax/src/Middleware/RetryMiddleware.php on line 183

sebastianoSali commented 10 months ago

I have same error on cloud-speech package.

fiboknacky commented 9 months ago

The gax-php repo has just been released last week. Could you please try again? Thanks.

fiboknacky commented 8 months ago

Closing due to inactivity.