PHP

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:

  1. GOOGLE_APPLICATION_CREDENTIALS environment variable.
  2. User credentials set up through the Google Cloud CLI (gcloud CLI).
  3. 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); }

For examples of other methods and resources, see the GitHub repository googleapis/php-ads-ad-manager.

Log HTTP requests and responses

The PHP client library supports PSR-3 compliant loggers for logging HTTP requests and responses. By default, logging is disabled.

To enable default logging to standard output, set the environment variable GOOGLE_SDK_PHP_LOGGING to true in either your PHP code or your environment:

putenv('GOOGLE_SDK_PHP_LOGGING=true');

$client = new NetworkServiceClient();
export GOOGLE_SDK_PHP_LOGGING=true

Alternatively, you can pass any PSR-3 compliant logger when constructing a service client:

use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;

$monologLogger = new Logger('sdk client');
$monologLogger->pushHandler(new StreamHandler('php://stdout', Level::Debug));

$client = new NetworkServiceClient([
    'logger' => $monologLogger
]);

Handle errors

In the PHP client library, all Ad Manager API errors throw an exception of type ApiException:

Parse errors

The error reason field uniquely identifies error types. Use this field to determine how to handle the error.

try {
    $response = $networkServiceClient->getNetwork($formattedName);
    printf('Response data: %s' . PHP_EOL, $response->serializeToJsonString());
} catch (ApiException $ex) {
    printf('Error message: %s' . PHP_EOL, $ex->getBasicMessage());
    printf('Reason: %s' . PHP_EOL, $ex->getReason());
}

Ad Manager API errors also include a unique request_id you can provide to support for assistance with troubleshooting. The following example extracts the request_id.

$requestInfo = null;
foreach ($ex->getMetadata() as $metadata) {
    if($metadata["@type"] === "type.googleapis.com/google.rpc.RequestInfo") {
        $requestInfo = $metadata;
        break;
    }
}
if ($requestInfo == null) {
    printf('Unexpected empty RequestInfo');
} else {
    printf('RequestId: %s' . PHP_EOL, $requestInfo['requestId']);
}

Construct resource names

The client library provides helper classes for building resource names from IDs.

use Google\Ads\AdManager\V1\Client\OrderServiceClient;

//  Constructs a String in the format:
//  "networks/{networkCode}/orders/{orderId}"
$orderName = OrderServiceClient::orderName("NETWORK_CODE", "ORDER_ID");

Configure proxy settings

The PHP client library respects HTTP_PROXY and HTTPS_PROXY environment settings.