AI-generated Key Takeaways
-
Google provides a PHP client library for interacting with the Ad Manager API, and it is recommended to use it with Composer.
-
The PHP client library uses OAuth2 and Application Default Credentials (ADC) for authentication, which search for credentials in specific locations.
-
You can make your first request by using the
ServiceClientobject for each service and its methods to interact with resources like theNetworkobject. -
The PHP client library supports logging HTTP requests and responses using PSR-3 compliant loggers, which can be enabled via an environment variable or by passing a logger object.
-
All Ad Manager API errors in the PHP client library throw an
ApiException, which includes a reason field to identify error types and a uniquerequest_idfor troubleshooting.
Google provides a PHP client library for interacting with the Ad Manager API. We recommend using the client library with Composer .
To get started, create a new project in the IDE of your choice or add the
dependency to an existing project. Google publishes client library artifacts to
Packagist as googleads/ad-manager
.
composer
require
googleads/ad-manager
Configure credentials
The PHP client library uses OAuth2 and Application Default Credentials (ADC) to authenticate.
ADC searches for credentials in order in the following locations:
-
GOOGLE_APPLICATION_CREDENTIALSenvironment variable. - User credentials set up through the Google Cloud CLI (gcloud CLI).
- When running on Google Cloud, the service account attached to the Google Cloud resource.
For creating and configuring your ADC credentials, see Authentication .
Make your first request
Each service has a ServiceClient
object with methods for each REST method. The
following example reads a Network
object.
< ?php
use Google\Ads\AdManager\V1\Client\NetworkServiceClient; use Google\Ads\AdManager\V1\GetNetworkRequest; use Google\Ads\AdManager\V1\Network; use Google\ApiCore\ApiException; /** * API to retrieve a Network object. * * @param string $formattedName Resource name of Network. * Format: networks/{network_code} * Please see {@see NetworkServiceClient::networkName()} for help formatting this field. */ function get_network_sample(string $formattedName): void { // Create a client. $networkServiceClient = new NetworkServiceClient(); // Prepare the request message. $request = (new GetNetworkRequest()) ->setName($formattedName); // Call the API and handle any network failures. try { /** @var Network $response */ $response = $networkServiceClient->getNetwork($request); printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString()); } catch (ApiException $ex) { printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); } } /** * Helper to execute the sample. * */ function callSample(): void { $formattedName = NetworkServiceClient::networkName(' NETWORK_CODE '); get_network_sample($formattedName); }

