idk / yii2-google-apiclient
官方 Google API PHP 客户端的 Yii2 封装
2.1.0
2021-10-06 09:02 UTC
Requires
- google/apiclient: 2.*
- ramsey/uuid: ^3.7
- yiisoft/yii2: ~2
This package is auto-updated.
Last update: 2024-09-06 15:21:17 UTC
README
官方 Google API PHP 客户端的 Yii2 封装。
此扩展包含
- 一个控制台实用程序,用于生成您的凭证文件
- 一个组件,负责身份验证,并提供对服务的访问
安装
推荐安装方法是通过 Packagist 和 Composer。运行以下命令安装包并将其添加到项目的 composer.json
中
composer require idk/yii2-google-apiclient
配置
凭证文件
为了使用此扩展,您需要一个 Google 应用的凭证文件。
您可以使用提供的控制台实用程序生成此文件
- 在
config/console.php
中配置模块
'bootstrap' => ['log', 'yii2gac'], 'modules' => [ 'yii2gac' => [ 'class' => 'idk\yii2\google\apiclient\Module', ], ],
- 使用 /configure 子命令
./yii yii2gac/configure <clientSecretPath> [api]
其中 clientSecretPath
是从 Google Console 获得的密钥 JSON 文件的路径,以及 api
是 API 标识符(如果未提供,将提示输入)。
组件
您可以通过在 Yii 配置数组的 components
索引中添加条目来使用任意数量的 Google_Service 实例。
以下是如何设置 GMail 的示例,下面提供了一个使用示例。
'components' => [ // .. 'google' => [ 'class' => 'idk\yii2\google\apiclient\components\GoogleApiClient', 'credentialsPath' => '@runtime/google-apiclient/auth.json', 'clientSecretPath' => '@runtime/google-apiclient/secret.json', ],
这将使您能够在应用中访问 GMail 认证服务 Yii::$app->google->getService()
。
示例使用
在 GMail 中显示最新消息的主题
$gmail = new \Google_Service_Gmail(Yii::$app->google->getService()); $messages = $gmail->users_messages->listUsersMessages('me', [ 'maxResults' => 1, 'labelIds' => 'INBOX', ]); $list = $messages->getMessages(); if (count($list) == 0) { echo "You have no emails in your INBOX .. how did you achieve that ??"; } else { $messageId = $list[0]->getId(); // Grab first Message $message = $gmail->users_messages->get('me', $messageId, ['format' => 'full']); $messagePayload = $message->getPayload(); $headers = $messagePayload->getHeaders(); echo "Your last email subject is: "; foreach ($headers as $header) { if ($header->name == 'Subject') { echo "<b>" . $header->value . "</b>"; } } }