Stay organized with collectionsSave and categorize content based on your preferences.
Google provides a PHP client library for interacting with the Ad Manager API.
We recommend using the client library withComposer.
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 asgoogleads/ad-manager.
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, seeAuthentication.
Make your first request
Each service has aServiceClientobject with methods for each REST method. The
following example reads aNetworkobject.
<?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);}
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 variableGOOGLE_SDK_PHP_LOGGINGtotruein either your PHP code or your environment:
putenv('GOOGLE_SDK_PHP_LOGGING=true');$client = new NetworkServiceClient();
exportGOOGLE_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 typeApiException:
Parse errors
The error reason field uniquely identifies error types. Use
this field to determine how to handle the error.
Ad Manager API errors also include a uniquerequest_idyou can
provide tosupportfor assistance with
troubleshooting. The following example extracts therequest_id.
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 respectsHTTP_PROXYandHTTPS_PROXYenvironment settings.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["Google provides a PHP client library for interacting with the Ad Manager API.\nWe recommend using the client library with [Composer](//getcomposer.org).\n\nTo get started, create a new project in the IDE of your choice or add the\ndependency to an existing project. Google publishes client library artifacts to\nPackagist as\n[`googleads/ad-manager`](//packagist.org/packages/googleads/ad-manager). \n\n composer require googleads/ad-manager\n\nConfigure credentials\n\nThe PHP client library uses OAuth2 and [Application Default Credentials](//cloud.google.com/docs/authentication/application-default-credentials)\n(ADC) to authenticate.\n\nADC searches for credentials in order in the following locations:\n\n1. `GOOGLE_APPLICATION_CREDENTIALS` environment variable.\n2. User credentials set up through the Google Cloud CLI (gcloud CLI).\n3. When running on Google Cloud, the service account attached to the Google Cloud resource.\n\nFor creating and configuring your ADC credentials, see\n[Authentication](/ad-manager/api/beta/authentication).\n\nMake your first request\n\nEach service has a `ServiceClient` object with methods for each REST method. The\nfollowing example reads a\n[`Network`](/ad-manager/api/beta/reference/rest/v1/networks) object. \n\n```php\n\u003c?php\n\nuse Google\\Ads\\AdManager\\V1\\Client\\NetworkServiceClient;\nuse Google\\Ads\\AdManager\\V1\\GetNetworkRequest;\nuse Google\\Ads\\AdManager\\V1\\Network;\nuse Google\\ApiCore\\ApiException;\n\n/**\n * API to retrieve a Network object.\n *\n * @param string $formattedName Resource name of Network.\n * Format: networks/{network_code}\n * Please see {@see NetworkServiceClient::networkName()} for help formatting this field.\n */\nfunction get_network_sample(string $formattedName): void\n{\n // Create a client.\n $networkServiceClient = new NetworkServiceClient();\n\n // Prepare the request message.\n $request = (new GetNetworkRequest())\n -\u003esetName($formattedName);\n\n // Call the API and handle any network failures.\n try {\n /** @var Network $response */\n $response = $networkServiceClient-\u003egetNetwork($request);\n printf('Response data: %s' . PHP_EOL, $response-\u003eserializeToJsonString());\n } catch (ApiException $ex) {\n printf('Call failed with message: %s' . PHP_EOL, $ex-\u003egetMessage());\n }\n}\n\n/**\n * Helper to execute the sample.\n *\n */\nfunction callSample(): void\n{\n $formattedName = NetworkServiceClient::networkName('\u003cvar label=\"network code\" translate=\"no\"\u003eNETWORK_CODE\u003c/var\u003e');\n\n get_network_sample($formattedName);\n} \nhttps://github.com/googleapis/google-cloud-php/blob/957aa127cea66650399059d56fd60635bf07a9d1/AdsAdManager/samples/V1/NetworkServiceClient/get_network.php#L26-L71\n```\n\nFor examples of other methods and resources, see the GitHub repository\n[`googleapis/php-ads-ad-manager`](//github.com/googleapis/php-ads-ad-manager/tree/main/samples/V1).\n\nLog HTTP requests and responses\n\nThe PHP client library supports PSR-3 compliant loggers\nfor logging HTTP requests and responses. By default, logging is disabled.\n\nTo enable default logging to standard output, set the environment variable\n`GOOGLE_SDK_PHP_LOGGING`\nto `true` in either your PHP code or your environment: \n\n```php\nputenv('GOOGLE_SDK_PHP_LOGGING=true');\n\n$client = new NetworkServiceClient();\n``` \n\n export GOOGLE_SDK_PHP_LOGGING=true\n\nAlternatively, you can pass any PSR-3 compliant logger when constructing\na service client: \n\n```php\nuse Monolog\\Handler\\StreamHandler;\nuse Monolog\\Level;\nuse Monolog\\Logger;\n\n$monologLogger = new Logger('sdk client');\n$monologLogger-\u003epushHandler(new StreamHandler('php://stdout', Level::Debug));\n\n$client = new NetworkServiceClient([\n 'logger' =\u003e $monologLogger\n]);\n```\n\nHandle errors\n\nIn the PHP client library, all Ad Manager API errors throw an exception of type\n[ApiException](//cloud.google.com/php/docs/reference/gax/latest/ApiException):\n\nParse errors\n\nThe error reason field uniquely identifies error types. Use\nthis field to determine how to handle the error. \n\n```php\ntry {\n $response = $networkServiceClient-\u003egetNetwork($formattedName);\n printf('Response data: %s' . PHP_EOL, $response-\u003eserializeToJsonString());\n} catch (ApiException $ex) {\n printf('Error message: %s' . PHP_EOL, $ex-\u003egetBasicMessage());\n printf('Reason: %s' . PHP_EOL, $ex-\u003egetReason());\n}\n```\n\nAd Manager API errors also include a unique `request_id` you can\nprovide to [support](/ad-manager/api/beta/support) for assistance with\ntroubleshooting. The following example extracts the\n`request_id`. \n\n```php\n$requestInfo = null;\nforeach ($ex-\u003egetMetadata() as $metadata) {\n if($metadata[\"@type\"] === \"type.googleapis.com/google.rpc.RequestInfo\") {\n $requestInfo = $metadata;\n break;\n }\n}\nif ($requestInfo == null) {\n printf('Unexpected empty RequestInfo');\n} else {\n printf('RequestId: %s' . PHP_EOL, $requestInfo['requestId']);\n}\n```\n\nConstruct resource names\n\nThe client library provides helper classes for building resource names from\nIDs. \n\n```php\nuse Google\\Ads\\AdManager\\V1\\Client\\OrderServiceClient;\n\n// Constructs a String in the format:\n// \"networks/{networkCode}/orders/{orderId}\"\n$orderName = OrderServiceClient::orderName(\"\u003cvar translate=\"no\"\u003eNETWORK_CODE\u003c/var\u003e\", \"\u003cvar translate=\"no\"\u003eORDER_ID\u003c/var\u003e\");\n```\n\nConfigure proxy settings\n\nThe PHP client library respects `HTTP_PROXY` and `HTTPS_PROXY`\nenvironment settings."]]