Protobuf implementations

  • Two PHP implementations for Protobuf are available: C (better performance, requires extension) and PHP (easier implementation, uses a package).

  • The C implementation is recommended when possible, but the PHP implementation can be used when necessary.

  • The PHP implementation is used automatically if the C implementation is not installed and configured.

  • Instructions are provided for installing and configuring both the C and PHP implementations.

  • A command is available to determine which Protobuf implementation is currently in use.

There are two Protobuf implementations available for PHP:

  1. C: Uses the Protobuf PHP C extension, which provides significantly better runtime performance.
  2. PHP: Uses the google/protobuf PHP package, which requires no additional system extensions.

Use the C implementation when possible, though the pure PHP implementation can be used when security or environment constraints prevent installing C extensions.

The PHP implementation is automatically installed as a dependency of googleapis/gax-php in Composer, and is used at runtime only if the C implementation is not installed and enabled.

C implementation

To install and configure the Protobuf PHP C extension:

  1. Install the extension using PECL:

    sudo pecl install protobuf
    
  2. Enable the extension in your active php.ini file (for both CLI and web server environments such as PHP-FPM or Apache; run php --ini to locate the CLI configuration file):

    extension=protobuf.so
    

PHP implementation

To use the pure PHP implementation, ensure that the C implementation is disabled and all dependencies of googleapis/gax-php are installed:

  1. Comment out or remove any extension=protobuf.so (or extension=php_protobuf.dll) line in your php.ini files.
  2. Run composer install from the directory that contains the composer.json file of your project. The composer.json file should specify googleads/google-ads-php as a dependency.

Determine which implementation is being used

Run the following command in your terminal to check whether the C extension is enabled in your PHP CLI environment:

php -i | grep protobuf
  • If the output is not empty (for example, protobuf support => enabled), you are using the C implementation.
  • Otherwise, you are not using the C implementation, and the Google Ads API PHP library automatically falls back to the pure PHP implementation installed by Composer.