- Notifications
You must be signed in to change notification settings - Fork451
Google Cloud Client Library for PHP
License
googleapis/google-cloud-php
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Idiomatic PHP client forGoogle Cloud Platform services.
View thelist of supported APIs and Services.
If you need support for other Google APIs, please check out theGoogle APIs Client Library for PHP.
We recommend installing individual component packages. A list of available packages can be found onPackagist.
For example:
$ composer require google/cloud-storage$ composer require google/cloud-bigquery$ composer require google/cloud-datastore
In this guide we'll focus on how to configure your client for local development using the GoogleCloud CLI (gcloud).
- Install the Google Cloud CLI.
- Authenticate with
gcloudto generate the credentials file. - Instantiate a client.
In order to generate our needed credentials file we need to authenticate to gcloud first.Installation is handled differently depending on your platform. Here is a link to help you setupthe Google Cloud CLI:
https://cloud.google.com/sdk/docs/install
Once the Google Cloud CLI tools are installed it is required that we authenticate via thegcloud:
$ gcloud init$ gcloud auth application-default login
This will create a local file in your system that the authentication library for our client willread in order to make requests to the apis with those credentials. This file is located in differentplace depending on your system.
Windows:
%APPDATA%\gcloud\application_default_credentials.jsonLinux and MacOS:
$HOME/.config/gcloud/application_default_credentials.jsonTo read more about Authentication, seeAUTHENTICATION.md
Install the Google Translate client library with composer
composer install google/cloud-translate
Note: For this example, we are using the Google Translate client library. Check thethe complete list of packages to find your requiredlibrary.
Now that we've authenticated and installed the client library, we can instantiate a client which willdetect the JSON file created by the gcloud CLI and use it to authenticate your requests:
require_once'vendor/autoload.php';useGoogle\Cloud\Translate\V3\Client\TranslationServiceClient;useGoogle\Cloud\Translate\V3\TranslateTextRequest;// Instantiating the client gathers the credentials from the `application_default_credentials.json`$client =newTranslationServiceClient([]);$request =newTranslateTextRequest();$request->setParent('projects/<YOUR_PROJECT_ID>');$request->setTargetLanguageCode('en-US');$request->setContents(['こんにちは']);// The request will contain the authentication token based on the default credentials file$response =$client->translateText($request);var_dump($response->getTranslations()[0]);// {// ["translatedText"]=>// string(5) "Hello"// ["detectedLanguageCode"]=>// string(2) "ja"// }
Please see ourDebugging guidefor more information about the debugging tools.
Many clients in Google Cloud PHP offer support for gRPC, either as an option or a requirement. gRPC is a high-performance RPC framework created by Google. To use gRPC in PHP, you must install the gRPC PHP extension on your server. While not required, it is also recommended that you install the protobuf extension whenever using gRPC in production.
$ pecl install grpc$ pecl install protobufBy default the library will use a simple in-memory caching implementation, however it is possible to override this behavior by passing aPSR-6 caching implementation in to the desired client.
The following example takes advantage ofSymfony's Cache Component.
require'vendor/autoload.php';useGoogle\Cloud\Storage\StorageClient;useSymfony\Component\Cache\Adapter\ArrayAdapter;// Please take the proper precautions when storing your access tokens in a cache no matter the implementation.$cache =newArrayAdapter();$storage =newStorageClient(['authCache' =>$cache]);
This library provides a PSR-6 implementation with the SystemV shared memory atGoogle\Auth\Cache\SysVCacheItemPool. This implementation is only available on *nix machines, but it's the one of the fastest implementations and you can share the cache among multiple processes. The following example shows how to use it.
require__DIR__ .'/vendor/autoload.php';useGoogle\Cloud\Spanner\SpannerClient;useGoogle\Auth\Cache\SysVCacheItemPool;$cache =newSysVCacheItemPool();$spanner =newSpannerClient(['authCache' =>$cache]);
All client libraries support PHP 8.1 and above.
This library followsSemantic Versioning.
Please note it is currently under active development. Any release versioned0.x.y is subject to backwards incompatible changes at any time.
GA: Libraries defined at a GA quality level are stable, and will notintroduce backwards-incompatible changes in any minor or patch releases. We willaddress issues and requests with the highest priority. Please note, for anycomponents which include generated clients the GA guarantee will only apply toclients which interact with stable services. For example, in a component whichhosts V1 and V1beta1 generated clients, the GA guarantee will only apply to theV1 client as the service it interacts with is considered stable.
Beta: Libraries defined at a Beta quality level are expected to be mostlystable and we're working towards their release candidate. We will address issuesand requests with a higher priority.
Contributions to this library are always welcome and highly encouraged.
SeeCONTRIBUTING for more information on how to get started.
This repository is not an official support channel. If you have support questions,file a support request through the normal Google support channels,or post questions on a forum such asStackOverflow.
Apache 2.0 - SeeLICENSE for more information.
About
Google Cloud Client Library for PHP
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.