Google Ads API Client Library for PHP

repository·main·Indexed 43 days ago

https://github.com/googleads/google-ads-php

A structured library for PHP applications to interact with the Google Ads API, handling authentication, service client instantiation, and data transport. Includes support for gRPC and Protobuf extensions, OAuth2 credential setup, and a sample Laravel application integration.

Tokens
1.7K
Snippets
5
Records
9
Agent score
56%

What's inside Google Ads PHP

  1. Set up the Laravel Sample Application

    main

    This sample application demonstrates how to integrate the Google Ads API into a Laravel environment. To run it, you must clone the repository, install dependencies via Composer within the specific sample directory, and configure your OAuth2 credentials.

    Prerequisites

    • A web server with a document root set to the application location (or Laravel Homestead).
    • System requirements for the Google Ads API Client Library for PHP must be met.
    • Valid OAuth2 credentials (follow the [Getting Started] guide in the main repository).

    Installation Steps

    1. Clone the repository:
      git clone https://github.com/googleads/google-ads-php.git
    2. Navigate to the sample application directory:
      cd google-ads-php/examples/LaravelSampleApp/
    3. Install dependencies:
      composer update
      Warning: Ensure you are inside the LaravelSampleApp directory before running composer update, otherwise you will install dependencies for the library instead of the application.
    4. Configure credentials:
      • Copy your configured google_ads_php.ini file into the examples/LaravelSampleApp/ directory.
    5. Start the application:
      php artisan serve
    6. Access the app via your browser (e.g., http://localhost:<PORT>).
    git clone https://github.com/googleads/google-ads-php.git
    cd google-ads-php/examples/LaravelSampleApp/
    composer update
    # Copy your google_ads_php.ini here
    php artisan serve
  2. Set up OAuth2 credentials for Google Ads API

    main

    The library requires OAuth2 credentials and a developer token to authenticate requests. Follow these steps to set up an installed application OAuth2 flow:

    1. Create Credentials: Follow the Google Cloud instructions to create an OAuth2 client ID and secret.
    2. Prepare Configuration File: Copy the sample google_ads_php.ini from examples/Authentication/google_ads_php.ini to your system's home directory.
    3. Generate User Credentials: Run the GenerateUserCredentials.php example. It will prompt you for your OAuth2 client ID and secret.
    4. Update INI File: Copy the output from the generation step into your google_ads_php.ini file in your home directory. Ensure you also include your developer token in this file.
  3. Access Google Ads API documentation and support

    main
    For detailed information on the Google Ads API itself, use the official Google Developers documentation. For community support and troubleshooting, use the Google Groups support forum or the GitHub issue tracker for the PHP client library.
  4. Configure gRPC and Protobuf PHP extensions

    main

    While the library can work without them, installing the grpc and protobuf extensions via PECL is highly recommended for performance and functionality.

    Install gRPC

    1. Install the extension:
      sudo pecl install grpc
    2. Add extension=grpc.so to your php.ini file.
    3. Verify installation:
      php -i | grep grpc

    Install Protobuf

    1. Install the extension:
      sudo pecl install protobuf
    2. Add extension=protobuf.so to your php.ini file.
    3. Verify installation:
      php -i | grep protobuf
    sudo pecl install grpc
    sudo pecl install protobuf
  5. Run Google Ads PHP code examples

    main

    To explore the library using the provided examples, follow these steps:

    1. Clone the repository:
      git clone https://github.com/googleads/google-ads-php.git
      cd google-ads-php
    2. Install dependencies:
      composer install
    3. Configure credentials (see Set up OAuth2 credentials).
    4. Execute an example (e.g., GetCampaigns): Pass your Google Ads customer ID (without dashes) as a command-line parameter:
      php examples/BasicOperations/GetCampaigns.php --customerId <YOUR_CUSTOMER_ID>

    Note: Most examples can be queried for required parameters using the --help flag.

    php examples/BasicOperations/GetCampaigns.php --customerId 1234567890
  6. Install the Google Ads PHP Client Library via Composer

    main

    To use the Google Ads API in your project, install the library using Composer. This will pull in the library and all necessary dependencies into your vendor/ directory.

    1. Navigate to your project's root directory.
    2. Run the following command:
    composer require googleads/google-ads-php
  7. Configure permissions for the Laravel Sample Application

    main

    When running the Laravel Sample Application, ensure the following permission settings are applied to avoid runtime errors:

    1. Directory Configuration: Follow standard Laravel directory configuration to ensure the web server has appropriate access to application folders.
    2. Logging Permissions: If you have enabled logging and specified log file paths in your google_ads_php.ini file, ensure your web server has write permissions for those specific file paths.
  8. Instantiate a GoogleAdsClient and get service clients

    main

    To interact with the Google Ads API, you must first instantiate a GoogleAdsClient. Once instantiated, you can access specific API services through the client's service client methods.

    1. Instantiate the client: Create an instance of GoogleAdsClient (configuration details are available in the Configuration guide).
    2. Get a service client: Use the get...ServiceClient() methods on your GoogleAdsClient instance to obtain the specific client needed for your task (e.g., getCampaignServiceClient()).
  9. Configure the development environment with Docker Compose

    main

    The project provides a docker-compose.yml file to set up a development environment using a pre-configured PHP base image.

    Key configuration details:

    • Image: Uses a base image from the Google Artifact Registry: us-central1-docker.pkg.dev/kokoro-container-bakery/google-ads-php/php${PHP_VERSION:-81}-base:latest.
    • PHP Version: The PHP version can be controlled via the PHP_VERSION environment variable. It defaults to 81 if not specified.
    • Volumes:
      • The current directory (./) is mounted to /google-ads-php inside the container.
      • The user's home directory (~/) is mounted to /root inside the container.
    version: '2'
    services:
      web:
        image: us-central1-docker.pkg.dev/kokoro-container-bakery/google-ads-php/php${PHP_VERSION:-81}-base:latest
        volumes:
          - ./:/google-ads-php
          - ~/:/root