Google Ad Manager SOAP API Client Library for PHP
Google Ad Manager SOAP API Client Library for PHP
This repository hosts the PHP client library for the Google Ad Manager SOAP API.
The AdWords API is sunset. You can use the new client library google-ads-php to access the new Google Ads API instead. IMPORTANT This client library has been updated to require PHP version 8.0 as the minimum version, as announced in #777. This means that the final version of the library that supports PHP 7 is v61.0.0, which supports Google Ad Manager API v202208, v202211, v202302, and v202305.
Google Ad Manager API v202305 is scheduled to be sunset in May 2024. Therefore, PHP 7 users have about a year to migrate to the version 8.0 or higher of PHP to avoid disruption.
composer.json of this
library. See this page for
more details.composer command will
download an older version of the googleads/googleads-php-lib library.composer.Copy the sample Ad Manager
adsapi_php.ini
to your
home directory
and fill out the required properties before downloading the library.
This library determines the home directory of your computer by using
EnvironmentalVariables::getHome().
There are three ways of downloading this library as described below.
| Method | Target Users |
|---|---|
Using composer require |
Those who want to use this library as a third-party library for their projects and thus example files are not needed. |
Using git clone |
Those who want to alter or contribute to this library (e.g., submitting a pull request) or want to run example files. |
| Downloading a compressed tarball | Those who only want to run example files. |
composer require is not an option since there are no
example files downloaded with the library.The steps below download this library as a third-party library for your projects.
The library will be downloaded by Composer and stored under the vendor/ directory.
Examples are NOT downloaded by this download method.
Install the latest version using Composer.
$ composer require googleads/googleads-php-libFollow Getting started if you haven't done so.
Follow Setting up your OAuth2 credentials if you haven't set up the credentials yet.
You can now use the library.
This method is suitable for those who want to alter or contribute to this library (e.g., submitting pull requests) or wish to try our examples. All files in this repository will be downloaded.
Run git clone https://github.com/googleads/googleads-php-lib.git at
the command prompt.
You'll get a googleads-php-lib directory. Navigate to it by running
cd googleads-php-lib.
Run composer install at the command prompt. This will install all
dependencies needed for using the library and running examples.
Follow Getting started if you haven't done so.
Follow Setting up your OAuth2 credentials if you haven't set up the credentials yet.
You can now use the library and run any examples you want. Try GetAllNetworks.php by executing the following command:
$ php examples/AdManager/v202211/NetworkService/GetAllNetworks.phpThis is suitable for those who only want to try out the Ad Manager API with this
client library. The extracted directory of the tarball will contain only the
examples/ directory.
On the releases page, select a version you want to try. Then, under Download, select the tarball of your choice. The name of the tarball indicates which product it belongs to, for example, admanager-examples-vX.Y.Z.tar.gz.
Extract your downloaded file to any location on your computer.
Navigate to the extracted directory (for example, admanager-examples-vX.Y.Z).
Run composer install at the command prompt. This will install all dependencies
needed for using the library and running examples.
Follow Getting started if you haven't done so.
Follow Setting up your OAuth2 credentials if you haven't set up the credentials yet.
You can now run any examples you want. Try GetAllNetworks.php by executing the following command:
$ php examples/AdManager/v202211/NetworkService/GetAllNetworks.phpThe Ad Manager API uses OAuth2 as the authentication mechanism. Follow the appropriate guide below based on your use case:
The best way to learn how to use this library is to review the examples.
All our examples are meant to be run via the command line and not through a webpage.
If you're using 32-bit PHP, you'll need to change all instances of
intval()tofloatval()before running an example. This is due to some IDs exceeding the 32-bitPHP_INT_MAXthatintval()changes your value to. In addition, when writing your own code, do not applyintval()on any attributes that are explicitly an integer.
The following snippet of code from the GetAllNetworks.php example gives you an idea of how to use this library.
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()
->build();
$session = (new AdManagerSessionBuilder())->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
$adManagerServices = new ServiceFactory();
$networkService = $adManagerServices->createNetworkService($session);
$networks = $networkService->getAllNetworks();
// Do something with the $networks.The builder's fromFile() method looks for an adsapi_php.ini file in your
home directory by default. If you want to store this file in another directory,
pass the path of the file as an argument. For example:
fromFile('/config/myprops.ini')It is highly recommended that you use an adsapi_php.ini file. However, if you
don't want to or can't use one, you can use the OAuth2 token and ads session
builders instead to set the same information. See the builders for details:
Some WSDL enum values have names that are reserved PHP keywords and need to be
modified when used as PHP constant names. For example, AND and DEFAULT are
generated as AND_VALUE and DEFAULT_VALUE.
This library conforms to PSR-3 for logging and provides the following loggers for Ad Manager:
In general, each logger logs a summary and debug message for events (e.g., a SOAP API call). The level at which messages are logged depends on whether the event succeeded.
| Log message \ Event status | Success | Failure |
|---|---|---|
| One-line summary | INFO | WARNING |
| Debug message (e.g., SOAP payload) | DEBUG | NOTICE |
By default, each of the library loggers logs to
STDERR on a
separate channel using a Monolog
StreamHandler.
You can configure some options for these default loggers in the adsapi_php.ini
file:
[LOGGING]
; Optional logging settings.
soapLogFilePath = "path/to/your/soap.log"
soapLogLevel = "NOTICE"If you need to further customize logging, you can specify your own logger entirely by providing a logger that implements LoggerInterface in the Ad Manager session builder:
$session = (new AdManagerSessionBuilder())
...
->withSoapLogger(new MyCustomSoapLogger())
->withReportDownloaderLogger(new MyCustomReportDownloaderLogger())
->build();We provide some utilities in this client library for helping you use features in the Ad Manager API more conveniently.
No open issues yet, or sync has not completed.