Google雲平台服務的慣用PHP客戶端。
| PHP版本 | 地位 |
|---|---|
查看支持的API和服務列表。
如果您需要支持其他Google API,請查看Google APIS客戶端庫以獲取PHP。
我們建議安裝單個組件軟件包。可以在Packagist上找到可用軟件包的列表。
例如:
$ composer require google/cloud-storage
$ composer require google/cloud-bigquery
$ composer require google/cloud-datastore在本指南中,我們將重點介紹如何使用Google Cloud Cli( gcloud )配置您的客戶端以進行本地開發。
gcloud進行身份驗證以生成憑據文件。為了生成我們所需的憑據文件,我們需要先對GCLOUD進行身份驗證。根據您的平台,安裝的處理方式不同。這是一個可幫助您設置Google Cloud CLI的鏈接:
https://cloud.google.com/sdk/docs/install
gcloud命令進行身份驗證安裝了Google Cloud CLI工具後,我們需要通過gcloud進行身份驗證:
$ gcloud init
$ gcloud auth application-default login這將在您的系統中創建一個本地文件,我們的客戶端身份驗證庫將閱讀,以便使用這些憑據向API提出請求。該文件位於不同位置,具體取決於您的系統。
視窗:
%APPDATA%gcloudapplication_default_credentials.json
Linux和MacOS:
$HOME/.config/gcloud/application_default_credentials.json
要了解有關身份驗證的更多信息,請參見Authentication.md
使用作曲家安裝Google Translate客戶庫
composer install google/cloud-translate注意:在此示例中,我們使用的是Google Translate客戶庫庫。檢查完整的軟件包列表以查找所需的庫。
現在,我們已經對客戶端庫進行了認證並安裝了客戶庫,我們可以實例化一個客戶端,該客戶端將檢測GCLOUD CLI創建的JSON文件並使用它來驗證您的請求:
require_once ' vendor/autoload.php ' ;
use Google Cloud Translate V3 Client TranslationServiceClient ;
use Google Cloud Translate V3 TranslateTextRequest ;
// Instantiating the client gathers the credentials from the `application_default_credentials.json`
$ client = new TranslationServiceClient ([]);
$ request = new TranslateTextRequest ();
$ 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"
// }該快速啟動是考慮到本地發展的。部署項目的步驟取決於您使用的環境。在這裡,我們提供了一些有關如何開始部署項目的基本指示:
有關獲取服務帳戶憑據的更多信息,請參閱我們的身份驗證指南。設置GOOGLE_APPLICATION_CREDENTIALS環境變量指向您的憑據文件。
一些客戶接受指向憑據文件的keyFilePath和keyFile配置選項:
require ' vendor/autoload.php ' ;
use Google Cloud Storage StorageClient ;
// Authenticate using a keyfile path
$ cloud = new StorageClient ([
' keyFilePath ' => ' path/to/keyfile.json '
]);
// Authenticate using keyfile data
$ cloud = new StorageClient ([
' keyFile ' => json_decode ( file_get_contents ( ' /path/to/keyfile.json ' ), true )
]);接受這些參數的客戶列表是:
我們建議您訪問您使用的客戶庫的客戶端文檔,以獲取更多深入信息。
如果您不希望將您的身份驗證信息嵌入應用程序代碼中,則也可以使用應用程序默認憑據。
require ' vendor/autoload.php ' ;
use Google Cloud Storage StorageClient ;
putenv ( ' GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json ' );
$ cloud = new StorageClient ();可以在服務器配置中設置GOOGLE_APPLICATION_CREDENTIALS環境變量。
請參閱我們的調試指南以獲取有關調試工具的更多信息。
Google Cloud PHP中的許多客戶為GRPC提供支持,無論是作為選項還是要求。 GRPC是由Google創建的高性能RPC框架。要在PHP中使用GRPC,必須在服務器上安裝GRPC PHP擴展名。雖然不需要,但還建議您在生產中使用GRPC時安裝Protobuf擴展。
$ pecl install grpc
$ pecl install protobuf
默認情況下,庫將使用簡單的內存緩存實現,但是可以通過將PSR-6緩存實現傳遞給所需的客戶端來覆蓋此行為。
以下示例利用了Symfony的高速緩存組件。
require ' vendor/autoload.php ' ;
use Google Cloud Storage StorageClient ;
use Symfony Component Cache Adapter ArrayAdapter ;
// Please take the proper precautions when storing your access tokens in a cache no matter the implementation .
$ cache = new ArrayAdapter ();
$ storage = new StorageClient ([
' authCache ' => $ cache
]);該庫在GoogleAuthCacheSysVCacheItemPool上提供了SystemV共享內存的PSR-6實現。此實現僅在 *NIX機器上可用,但它是最快的實現之一,您可以在多個進程之間共享緩存。以下示例顯示瞭如何使用它。
require __DIR__ . ' /vendor/autoload.php ' ;
use Google Cloud Spanner SpannerClient ;
use Google Auth Cache SysVCacheItemPool ;
$ cache = new SysVCacheItemPool ();
$ spanner = new SpannerClient ([
' authCache ' => $ cache
]);所有客戶庫均支持PHP 8.0及更高版本。
該庫遵循語義版本控制。
請注意,目前正在積極開發。任何發行版本均為0.XY的任何版本都會隨時進行向後不兼容的更改。
GA :定義在GA質量水平的庫是穩定的,不會在任何次要或補丁發行版中引入向後不兼容的更改。我們將解決優先級最高的問題和請求。請注意,對於包括生成客戶的任何組件,GA保證僅適用於與穩定服務互動的客戶。例如,在託管V1和V1BETA1生成的客戶端的組件中,GA保證僅適用於V1客戶端,因為它與之交互的服務被認為是穩定的。
Beta :預計以Beta質量級別定義的圖書館大多是穩定的,我們正在努力為其發布候選人努力。我們將以更高的優先級解決問題和請求。
總是歡迎和鼓勵對該圖書館的貢獻。
有關如何入門的更多信息,請參見貢獻。
該存儲庫不是官方的支持渠道。如果您有支持問題,請通過普通的Google支持渠道提交支持請求,或在stackoverflow等論壇上發布問題。
Apache 2.0-有關更多信息,請參見許可證。