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
294 stars 262 forks source link

Fatal error: Uncaught Error: Class "Google\Ads\GoogleAds\Examples\Utils\ArgumentParser" not found in GetCampaigns.php #760

Closed publish-web closed 2 years ago

publish-web commented 2 years ago

Hi everyone,

I'm facing an issue with Ads API. When I call GetCampaigns.php, I'm get this error :

Fatal error: Uncaught Error: Class "Google\Ads\GoogleAds\Examples\Utils\ArgumentParser" not found in /var/www/html/GetCampaigns.php:31 Stack trace: #0 /var/www/html/GetCampaigns.php(107): Google\Ads\GoogleAds\Examples\BasicOperations\GetCampaigns::main() #1 {main} thrown in /var/www/html/GetCampaigns.php on line 31

But it works well in command line with : php examples/BasicOperations/GetCampaigns.php --customerId

Anyone knows why ?

Thank you!

Kenimartonia commented 2 years ago

Ok thanks and best regards

On Wed, Mar 9, 2022, 12:36 AM publish-web @.***> wrote:

Hi everyone,

I'm facing an issue with Ads API. When I call GetCampaigns.php, I'm get this error :

Fatal error: Uncaught Error: Class "Google\Ads\GoogleAds\Examples\Utils\ArgumentParser" not found in /var/www/html/GetCampaigns.php:31 Stack trace: #0 /var/www/html/GetCampaigns.php(107): Google\Ads\GoogleAds\Examples\BasicOperations\GetCampaigns::main() #1 https://github.com/googleads/google-ads-php/pull/1 {main} thrown in /var/www/html/GetCampaigns.php on line 31

But it works well in command line with : php examples/BasicOperations/GetCampaigns.php --customerId

Anyone knows why ? Thank you! - Client library version: v10 - Google Ads API version: v14.0.0 Ubuntu / PHP 8.0 — Reply to this email directly, view it on GitHub , or unsubscribe . Triage notifications on the go with GitHub Mobile for iOS or Android . You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
PierrickVoulet commented 2 years ago

Hi @publish-web,

The file you are reusing is an example that was developed to execute from command line only. To reuse its content in the context of your web application you will need to adapt it because of the development sources and dependencies it uses.

If you are interested by a similar example running as a web application, you can take a look at the Laravel sample application.

Thanks,

publish-web commented 2 years ago

Hi @PierrickVoulet

Thanks for your answer. I don't use Laravel but a custom web application. I couldn't find any example by following the link you gave me, sorry.

I currently use the Adwords API and want to migrate to the Ads API.

Is there some examples running as a web application ?

Thanks :)

PierrickVoulet commented 2 years ago

The only web application example we have is the LaravelSampleApp, you may want to check how the controller is implemented to learn how to use this library in this context.

To help you get started, here is a minimalist code example for your convenience. If should run successfully in your application if you already added this library as a dependency. It does the same thing as the code example GetCampaigns but feel free to change it to your convenience:

use Google\Ads\GoogleAds\Lib\V10\GoogleAdsClientBuilder;
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder;

/** @var int $customerId Change this value to the customer ID to request campaigns for */
$customerId = null;
/** Change this value to the path of the Google Ads configuration file to use */
$googleAdsIniFilepath = '/path/to/google_ads_php.ini';

$oAuth2Credential = (new OAuth2TokenBuilder())
    ->fromFile($googleAdsIniFilepath)
    ->build();
$googleAdsClient = (new GoogleAdsClientBuilder())
    ->fromFile($googleAdsIniFilepath)
    ->withOAuth2Credential($oAuth2Credential)
    ->build();

$query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
$response = $googleAdsClient->getGoogleAdsServiceClient()->search($customerId, $query);
foreach ($response->iterateAllElements() as $googleAdsRow) {
    printf(
        "Campaign with ID %d and name '%s' was found.%s",
        $googleAdsRow->getCampaign()->getId(),
        $googleAdsRow->getCampaign()->getName(),
        PHP_EOL
    );
}
publish-web commented 2 years ago

Thanks Pierrick!

Je crois savoir que vous parlez français, je me permets de passer en français cela sera plus facile pour moi.

J'avais essayé d'adapter le code d'origine de GetCampaigns.php avant de poster ma précédente réponse mais j'obtenais la même erreur qu'avec votre code :

Fatal error: Uncaught Error: Class "Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder" not found

C'est étrange car le fichier de classe est bien présent : /google-ads-php/src/Google/Ads/GoogleAds/Lib/OAuth2TokenBuilder.php

Je pense avoir correctement installé la librairie avec composer... J'ai rajouté en début de code le require pour l'autoload aussi bien sûr.

Merci :)

PierrickVoulet commented 2 years ago

Il est difficile de déterminer ce qui pourrait ne pas fonctionner sur votre configuration sans y avoir un accès direct. Autant que je sache, les étapes d'installation doivent être exactement les mêmes que celles que vous avez utilisées pour la bibliothèque d'AdWords.

Je ne pense pas que ce problème soit spécifique à notre bibliothèque, mais n'hésitez pas à nous faire savoir si vous trouvez quelque chose que nous faisons mal et nous vous aiderons!

publish-web commented 2 years ago

J'ai fini par trouver... En fait la librairie n'était pas bien installée via composer. Il fallait lancer la commande "composer require googleads/google-ads-php" pour qu'elle figure bien dans composer.json notamment.

Maintenant cela fonctionne !

Sauf erreur de ma part cela ne figure pas dans le "Getting started" de cette doc : https://github.com/googleads/google-ads-php (je précise que je ne suis pas très familiarisé avec l'utilisation de composer, ceci explique peut-être cela ^^)

En tout cas merci malgré tout pour votre aide ;)

PierrickVoulet commented 2 years ago

Merci d'avoir confirmé ce qui a résolu le problème ! Nous garderons un œil sur cela et nous modifierons la documentation si d'autres personnes rencontrent ce problème à l'avenir.