google/cloud-core
Core infrastructure library for Google Cloud PHP clients. Provides shared components like authentication, retries, request handling, and utilities used across Google Cloud service packages. Not intended for direct use; typically installed as a dependency.
Installation:
composer require google/cloud-core
Note: This package is not used directly—it’s a dependency for other Google Cloud PHP packages (e.g., google/cloud-storage, google/cloud-bigquery). Install the specific service package instead.
First Use Case: Initialize a Google Cloud client (e.g., Storage) with authentication:
use Google\Cloud\Core\Authentication\CredentialsLoader;
use Google\Cloud\Storage\StorageClient;
$credentials = (new CredentialsLoader())->makeCredentials();
$storage = new StorageClient(['credentials' => $credentials]);
GOOGLE_APPLICATION_CREDENTIALS (JSON key file path).~/.config/gcloud/application_default_credentials.json).Where to Look First:
putenv('GOOGLE_CLOUD_DEBUG=1'); // Logs HTTP requests/responses
google/cloud-storage or google/cloud-bigquery, which build on google/cloud-core.// app/Providers/GoogleCloudServiceProvider.php
public function register()
{
$this->app->singleton('google.credentials', function () {
return (new \Google\Cloud\Core\Authentication\CredentialsLoader())
->makeCredentials();
});
}
$storage = new \Google\Cloud\Storage\StorageClient([
'credentials' => $this->app->make('google.credentials'),
]);
.env for flexibility:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
$client = new \Google\Cloud\PubSub\PubSubClient(); // Automatically uses ADC
$storage = new \Google\Cloud\Storage\StorageClient([
'retry' => [
'max_attempts' => 3,
'timeout' => 30.0, // seconds
],
]);
Google\Cloud\Core\Retry\RetryPolicy for service-specific rules.Google\Cloud\Core\Idempotency\IdempotencyMiddleware for APIs supporting idempotent operations (e.g., Pub/Sub):
$pubsub = new \Google\Cloud\PubSub\PubSubClient([
'middleware' => [new \Google\Cloud\Core\Idempotency\IdempotencyMiddleware()],
]);
use Google\Cloud\Core\ApiHelperTrait;
class MyService {
use ApiHelperTrait;
public function upload($bucket, $object, $source) {
$options = [
'name' => $object,
'source' => $source,
];
$this->validateOptions($options, [
'name' => 'string',
'source' => 'string|resource',
]);
// Proceed with upload
}
}
Google\Cloud\Core\OptionsValidator\ValidatorInterface for complex rules.putenv('GOOGLE_CLOUD_DEBUG=1');
putenv('GOOGLE_CLOUD_DEBUG_LEVEL=5'); // Verbose (0–5)
stderr (useful for Cloud Logging integration).use Google\Cloud\Core\Trace\TraceContext;
$traceContext = new TraceContext();
$traceContext->addSpan('my_operation', ['custom_attribute' => 'value']);
$request->headers->set('x-cloud-trace-context', $traceContext->toHeader());
$firestore = new \Google\Cloud\Firestore\FirestoreClient([
'emulatorHost' => 'localhost:8080',
'projectId' => 'my-project',
]);
GOOGLE_CLOUD_PROJECT for emulator projects.keyFile/keyFilePath OptionsGOOGLE_APPLICATION_CREDENTIALS.
// ❌ Deprecated (still works but warns)
$client = new \Google\Cloud\Storage\StorageClient([
'keyFile' => '/path/to/key.json',
]);
$credentials = (new \Google\Cloud\Core\Authentication\CredentialsLoader())
->fromFile('/path/to/service-account.json');
storage.objects.get) cause Google\Cloud\Core\Exception\GoogleException.
gcloud auth print-access-token to test permissions locally.retry at the client level (not per-operation):
$client = new \Google\Cloud\BigQuery\BigQueryClient([
'retry' => ['max_attempts' => 5],
]);
$client->upload(
$bucket,
$object,
$source,
['timeout' => 300] // 5 minutes
);
validateOptions() throws Google\Cloud\Core\Exception\InvalidArgumentException for invalid types.
$this->validateOptions($options, [
'name' => 'string|null',
'source' => 'string|resource|array',
]);
Google\Cloud\Core\Protobuf\Message for validation:
$message = new \Google\Cloud\BigQuery\V2\QueryRequest();
$message->setQuery('SELECT * FROM `dataset.table`');
GOOGLE_CLOUD_DEBUG=1 adds significant overhead (~20–30% latency).
if (app()->environment('production')) {
putenv('GOOGLE_CLOUD_DEBUG=0');
}
$object = $storage->bucket($bucket)->object($object);
How can I help you explore Laravel packages today?