Back

Step by Step Guide to Building a Google Ads API Integration in PHP

Aug 1, 20245 minute read

Introduction

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!

Prerequisites

Before we jump in, make sure you've got:

  • PHP 7.4 or higher (come on, it's 2023!)
  • Composer installed (because who wants to manage dependencies manually?)
  • Google Ads API credentials (if you don't have these yet, head over to the Google Ads API Center)

Installation

First things first, let's get that package installed:

composer require googleads/google-ads-php

Easy peasy, right?

Configuration

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.

Authentication

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();

Basic API Request

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

Common Operations

Now that you've got the basics down, let's look at some common operations:

Retrieving Account Information

$query = 'SELECT customer.id, customer.descriptive_name FROM customer'; // ... execute query as shown above

Creating a Campaign

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

Error Handling

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

Best Practices

  1. Rate Limiting: Be nice to the API. Implement exponential backoff for retries.
  2. Logging: Log everything. Your future self will thank you.

Conclusion

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!