pulkitjalan / google-apiclient
支持云平台和Laravel的Google API PHP客户端包装器
6.3.0
2024-07-01 05:53 UTC
Requires
- php: >=8.0
- google/apiclient: ^2.16
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.6
- pestphp/pest: ^1.20|^2.0
README
支持云平台和Laravel的Google API PHP客户端包装器
要求
- PHP >=8.0
安装
通过composer安装
composer require pulkitjalan/google-apiclient
Laravel
在Laravel中使用,请将以下内容添加到您的config/app.php
文件中的providers
数组中
PulkitJalan\Google\GoogleServiceProvider::class
然后,将以下内容添加到您的config/app.php
文件中的aliases
数组中
'Google' => PulkitJalan\Google\Facades\Google::class
最后,运行php artisan vendor:publish --provider="PulkitJalan\Google\GoogleServiceProvider" --tag="config"
以发布配置文件。
使用较旧的PHP / Laravel版本?
如果您使用的是低于8.0版本的PHP或低于10.0版本的Laravel,请使用此包的较旧版本。
用法
Client
类接受一个数组作为第一个参数,请参阅以下配置文件的示例
return [ /* |---------------------------------------------------------------------------- | Google application name |---------------------------------------------------------------------------- */ 'application_name' => '', /* |---------------------------------------------------------------------------- | Google OAuth 2.0 access |---------------------------------------------------------------------------- | | Keys for OAuth 2.0 access, see the API console at | https://developers.google.com/console | */ 'client_id' => '', 'client_secret' => '', 'redirect_uri' => '', 'scopes' => [], 'access_type' => 'online', 'prompt' => 'auto', /* |---------------------------------------------------------------------------- | Google developer key |---------------------------------------------------------------------------- | | Simple API access key, also from the API console. Ensure you get | a Server key, and not a Browser key. | */ 'developer_key' => '', /* |---------------------------------------------------------------------------- | Google service account |---------------------------------------------------------------------------- | | Set the credentials JSON's location to use assert credentials, otherwise | app engine or compute engine will be used. | */ 'service' => [ /* | Enable service account auth or not. */ 'enabled' => false, /* | Path to service account json file */ 'file' => '', ], /* |---------------------------------------------------------------------------- | Additional config for the Google Client |---------------------------------------------------------------------------- | | Set any additional config variables supported by the Google Client | Details can be found here: | https://github.com/google/google-api-php-client/blob/master/src/Google/Client.php | | NOTE: If client id is specified here, it will get over written by the one above. | */ 'config' => [], ];
要使用Google Cloud Platform服务,输入服务账户JSON文件的路径(不是JSON字符串本身)。要使用App Engine或Compute Engine,请留空。
有关Google的升级文档,请参阅升级文档
注意:P12s已被服务账户JSON所取代,这可以在Google开发者控制台中的凭据部分生成。
获取Google\Client
$client = new PulkitJalan\Google\Client($config); $googleClient = $client->getClient();
Laravel示例
$googleClient = Google::getClient();
获取服务
$client = new PulkitJalan\Google\Client($config); // returns instance of \Google\Service\Storage $storage = $client->make('storage'); // list buckets example $storage->buckets->listBuckets('project id'); // get object example $storage->objects->get('bucket', 'object');
Laravel示例
// returns instance of \Google\Service\Storage $storage = Google::make('storage'); // or $storage = Google::make(\Google\Service\Storage::class); // list buckets example $storage->buckets->listBuckets('project id'); // get object example $storage->objects->get('bucket', 'object');
请查看google/google-api-php-client-services以获取支持的Google服务的完整列表。