ajkosh/yii2-google-apiclient

官方 Google API PHP Client 的 Yii2 包装器

安装: 7

依赖项: 0

建议者: 0

安全: 0

星标: 0

分支: 0

类型:yii2-extension

1.1.0 2017-11-17 12:00 UTC

This package is not auto-updated.

Last update: 2024-09-26 19:07:51 UTC


README

官方 Google API PHP Client 的 Yii2 包装器。

Latest Stable Version Latest Unstable Version License Total Downloads

此扩展包括

  • 用于生成您的凭证文件的控制台实用程序
  • 一个组件,负责身份验证,并为您提供对服务的访问

安装

推荐的安装方法是使用 PackagistComposer。运行以下命令安装包并将其添加到项目的 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>";
        }
    }

}