|
| 1 | +# Configuring Client Options for Google Cloud PHP |
| 2 | + |
| 3 | +The Google Cloud PHP Client Libraries (built on `google/gax` and `google/cloud-core`) allow you to configure client behavior via an associative array passed to the client constructor. This array is processed by the [`Google\ApiCore\ClientOptions`](https://docs.cloud.google.com/php/docs/reference/gax/latest/Options.ClientOptions) class. |
| 4 | + |
| 5 | +## Common Configuration Options |
| 6 | + |
| 7 | +The following options can be passed to the constructor of any generated client (e.g., `PubSubClient`, `SpannerClient`, `StorageClient`). |
| 8 | + |
| 9 | +| Option Key | Type | Description | |
| 10 | +| ----- | ----- | ----- | |
| 11 | +| `credentials` | `string` | `array` | |
| 12 | +| `apiKey` | `string` | An **API Key** for services that support public API key authentication (bypassing OAuth2). | |
| 13 | +| `apiEndpoint` | `string` | The address of the API remote host. specific for **Regional Endpoints** (e.g., `us-central1-pubsub.googleapis.com:443`) or Private Service Connect. | |
| 14 | +| `transport` | `string` | Specifies the transport type. Options: `'grpc'` (default), `'rest'`, or `'grpc-fallback'`. | |
| 15 | +| `transportConfig` | `array` | Configuration specific to the transport, such as gRPC channel arguments. | |
| 16 | +| `disableRetries` | `bool` | If `true`, disables the default retry logic for all methods in the client. | |
| 17 | +| `logger` | `Psr\Log\LoggerInterface` | A PSR-3 compliant logger for client-level logging and tracing. | |
| 18 | +| `universeDomain` | `string` | Overrides the default service domain (defaults to `googleapis.com`) for Cloud Universe support. | |
| 19 | + |
| 20 | +## 1\. Authentication Configuration |
| 21 | + |
| 22 | +While the client attempts to find "Application Default Credentials" automatically, you can explicitly provide them using |
| 23 | +the `credentials` or `apiKey` options. See [`Authentication`][authentication.md] for details and examples. |
| 24 | + |
| 25 | +[authentication.md]: https://cloud.google.com/php/docs/reference/help/authentication |
| 26 | + |
| 27 | +## 2\. Customizing the API Endpoint |
| 28 | + |
| 29 | +You can modify the API endpoint to connect to a specific Google Cloud region (to reduce latency or meet data residency requirements) or to a private endpoint (via Private Service Connect). |
| 30 | + |
| 31 | +### Connecting to a Regional Endpoint |
| 32 | + |
| 33 | +Some services, like Pub/Sub and Spanner, offer regional endpoints. |
| 34 | + |
| 35 | +```php |
| 36 | +use Google\Cloud\PubSub\PubSubClient; |
| 37 | + |
| 38 | +$pubsub = new PubSubClient([ |
| 39 | + // Connect explicitly to the us-east1 region |
| 40 | + 'apiEndpoint' => 'us-east1-pubsub.googleapis.com:443', |
| 41 | +]); |
| 42 | +``` |
| 43 | + |
| 44 | +## 3\. Configuring a Proxy |
| 45 | + |
| 46 | +The configuration method depends on whether you are using the `grpc` (default) or `rest` transport. |
| 47 | + |
| 48 | +### Proxy with gRPC |
| 49 | + |
| 50 | +When using the gRPC transport, the client library respects the [standard environment variables](https://grpc.github.io/grpc/php/md_doc_environment_variables.html). You **do not** need to configure this in the PHP code itself. |
| 51 | + |
| 52 | +Set the following environment variables in your shell or Docker container: |
| 53 | + |
| 54 | +``` |
| 55 | +export http_proxy="http://proxy.example.com:3128" |
| 56 | +export https_proxy="http://proxy.example.com:3128" |
| 57 | +``` |
| 58 | + |
| 59 | +**Handling Self-Signed Certificates (gRPC):** If your proxy uses a self-signed certificate (Deep Packet Inspection), you cannot simply "ignore" verification in gRPC. You must provide the path to the proxy's CA certificate bundle. |
| 60 | + |
| 61 | +``` |
| 62 | +# Point gRPC to a CA bundle that includes your proxy's certificate |
| 63 | +export GRPC_DEFAULT_SSL_ROOTS_FILE_PATH="/path/to/roots.pem" |
| 64 | +``` |
| 65 | + |
| 66 | +### Proxy with REST |
| 67 | + |
| 68 | +If you are forcing the `rest` transport (or using a library that only supports REST), you must configure the proxy via the `transportConfig` option. This passes the settings down to the underlying Guzzle client. |
| 69 | + |
| 70 | +```php |
| 71 | +use Google\Auth\HttpHandler\HttpHandlerFactory; |
| 72 | +use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; |
| 73 | + |
| 74 | +$httpClient = new GuzzleHttp\Client([ |
| 75 | + // Standard Guzzle proxy configuration |
| 76 | + 'proxy' => 'http://user:password@proxy.example.com', |
| 77 | + // (Optional) Disable SSL Verification (Development Only) |
| 78 | + // 'verify' => false |
| 79 | +]); |
| 80 | +$httpHandler = HttpHandlerFactory::build($httpClient); |
| 81 | +$secretManagerClient = new SecretManagerServiceClient([ |
| 82 | + 'transport' => 'rest', |
| 83 | + 'transportConfig' => ['rest' => ['httpHandler' => [$httpHandler, 'async']]], |
| 84 | +]); |
| 85 | +``` |
| 86 | + |
| 87 | +## 4\. Configuring Retries and Timeouts |
| 88 | + |
| 89 | +There are two ways to configure retries and timeouts: global client configuration (complex) and per-call configuration (simple). |
| 90 | + |
| 91 | +### Per-Call Configuration (Recommended) |
| 92 | + |
| 93 | +For most use cases, it is cleaner to override settings for specific calls using `Google\ApiCore\Options\CallOptions` (or the `$optionalArgs` array in generated clients). |
| 94 | + |
| 95 | +#### Available `retrySettings` Keys |
| 96 | + |
| 97 | +When passing an array to `retrySettings`, you can use the following keys to fine-tune the exponential backoff strategy: |
| 98 | + |
| 99 | +| Key | Type | Description | |
| 100 | +| ----- | ----- | ----- | |
| 101 | +| `retriesEnabled` | `bool` | Enables or disables retries for this call. | |
| 102 | +| `maxRetries` | `int` | The maximum number of retry attempts. | |
| 103 | +| `initialRetryDelayMillis` | `int` | Wait time before the *first* retry (in ms). | |
| 104 | +| `retryDelayMultiplier` | `float` | Multiplier applied to the delay after each failure (e.g., `1.5`). | |
| 105 | +| `maxRetryDelayMillis` | `int` | The maximum wait time between any two retries. | |
| 106 | +| `totalTimeoutMillis` | `int` | Total time allowed for the request (including all retries) before giving up. | |
| 107 | + |
| 108 | +#### Example: Advanced Backoff |
| 109 | + |
| 110 | +```php |
| 111 | +// Advanced Retry Configuration |
| 112 | +$callOptions = [ |
| 113 | + 'retrySettings' => [ |
| 114 | + 'retriesEnabled' => true, |
| 115 | + 'maxRetries' => 3, |
| 116 | + 'initialRetryDelayMillis' => 500, // Start with 0.5s wait |
| 117 | + 'retryDelayMultiplier' => 2.0, // Double the wait each time (0.5s -> 1s -> 2s) |
| 118 | + 'maxRetryDelayMillis' => 5000, // Cap wait at 5s |
| 119 | + 'totalTimeoutMillis' => 15000 // Max 15s total |
| 120 | + ] |
| 121 | +]; |
| 122 | + |
| 123 | +$secretManagerClient->accessSecretVersion($request, $callOptions); |
| 124 | +``` |
| 125 | + |
| 126 | +### Disabling Retries |
| 127 | + |
| 128 | +You can also configure retries globally by passing a `clientConfig` array to the constructor. This is useful if you want to change the default retry strategy for *all* calls made by that client instance. |
| 129 | + |
| 130 | +```php |
| 131 | +use Google\Cloud\PubSub\PubSubClient; |
| 132 | + |
| 133 | +$pubsub = new PubSubClient([ |
| 134 | + // Quickly disable retries for the entire client |
| 135 | + 'disableRetries' => true |
| 136 | +]); |
| 137 | +``` |
| 138 | + |
| 139 | +## 5\. Logging |
| 140 | + |
| 141 | +You can attach any PSR-3 compliant logger (like Monolog) to debug request headers, status codes, and payloads. See [Debug Logging](https://docs.cloud.google.com/php/docs/reference/help/debug) for more examples. |
| 142 | + |
| 143 | +```php |
| 144 | +use Google\Cloud\PubSub\PubSubClient; |
| 145 | +use Monolog\Logger; |
| 146 | +use Monolog\Handler\StreamHandler; |
| 147 | + |
| 148 | +$logger = new Logger('google-cloud'); |
| 149 | +$logger->pushHandler(new StreamHandler('php://stderr', Logger::DEBUG)); |
| 150 | + |
| 151 | +$client = new PubSubClient([ |
| 152 | + 'logger' => $logger |
| 153 | +]); |
| 154 | +``` |
| 155 | + |
0 commit comments