Hey there, fellow developer! Ready to dive into the world of Google Ads API integration? You're in the right place. We'll be using the googleads/google-ads-php
package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
First things first, let's get that package installed:
composer require googleads/google-ads-php
Easy peasy, right?
Now, let's set up our google_ads_php.ini
file. This is where the magic happens:
[GOOGLE_ADS] developerToken = "YOUR_DEVELOPER_TOKEN" loginCustomerId = "1234567890" [OAUTH2] clientId = "YOUR_CLIENT_ID" clientSecret = "YOUR_CLIENT_SECRET" refreshToken = "YOUR_REFRESH_TOKEN"
Replace those placeholders with your actual credentials. Don't worry, we'll get to that refresh token in a sec.
OAuth 2.0 is the name of the game here. To get your refresh token, you'll need to implement the OAuth flow. Here's a quick snippet to get you started:
use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; $oAuth2Credential = (new OAuth2TokenBuilder()) ->fromFile('path/to/google_ads_php.ini') ->build();
Let's make our first API request. Exciting, right?
use Google\Ads\GoogleAds\Lib\V13\GoogleAdsClient; use Google\Ads\GoogleAds\V13\Services\GoogleAdsRow; $googleAdsClient = (new GoogleAdsClientBuilder()) ->fromFile('path/to/google_ads_php.ini') ->withOAuth2Credential($oAuth2Credential) ->build(); $customerId = 'INSERT_CUSTOMER_ID_HERE'; $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id'; $stream = $googleAdsClient->getGoogleAdsServiceClient()->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(), $googleAdsRow->getCampaign()->getName(), PHP_EOL ); }
Now that you've got the basics down, let's look at some common operations:
$query = 'SELECT customer.id, customer.descriptive_name FROM customer'; // ... execute query as shown above
use Google\Ads\GoogleAds\V13\Resources\Campaign; use Google\Ads\GoogleAds\V13\Enums\CampaignStatusEnum\CampaignStatus; $campaign = new Campaign([ 'name' => 'My First Campaign', 'status' => CampaignStatus::PAUSED, // ... other campaign settings ]); // Use the CampaignService to create the campaign
Always wrap your API calls in try-catch blocks. The Google Ads API can throw some pretty specific exceptions:
use Google\Ads\GoogleAds\V13\Errors\GoogleAdsException; try { // Your API call here } 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); }
And there you have it! You're now equipped to start building your Google Ads API integration in PHP. Remember, the official documentation is your best friend for more advanced topics and edge cases.
Happy coding, and may your campaigns be ever successful!