【问题标题】:How to retrieve member list from Silverstripe API如何从 Silverstripe API 中检索成员列表
【发布时间】:2020-02-26 21:36:15
【问题描述】:

我正在开发一个平台,我需要从现有的 Silverstripe 安装中获取成员电子邮件。我认为是 V4 但还不确定。

我希望调用 REST API,但我似乎找不到任何有关您将如何执行此操作的信息。我需要每天打电话来获取最新的成员。

这是可能的还是有其他方法可以做到这一点?

我查看了 API 文档,但信息没有帮助,也没有解释或示例。 https://api.silverstripe.org/4/index.html

【问题讨论】:

    标签: silverstripe


    【解决方案1】:

    Silverstripe 4 不通过开箱即用的 REST API 服务公开其数据。我们可以安装一个模块来允许我们这样做。

    Rest API 模块: https://github.com/colymba/silverstripe-restfulapi

    Rest API 模块: https://github.com/silverstripe/silverstripe-restfulserver

    另一种方法是使用 Silverstripe GraphQL 模块来检索数据: https://github.com/silverstripe/silverstripe-graphql

    【讨论】:

      【解决方案2】:

      您可以使用 SilverStripe 4 中的 SilverStripe GraphQL API 开箱即用地执行此操作。除了3dgoo's answer,这里还有一些指导:

      开箱即用的配置

      SilverStripe 公开了一个“管理员”GraphQL 服务器,需要您登录才能使用它。如果您想从另一台服务器使用它,您可以对您的基本身份验证凭据进行 base64 编码并将其作为标头传递。 More info on this here.

      SilverStripe CMS 模块already exposes member's first and last names,因为它们已经被部分 CMS 通过 GraphQL API 使用。如果你想要一个电子邮件地址,那么你可以在 app 文件夹中添加一些基本的 YAML。

      添加会员的邮箱

      将一些自定义配置添加到您的 app/_config 文件夹 - SilverStripe 中的配置已合并,因此数组值 fields: [Email] 将与上面链接的 CMS 值合并

      # File: app/_config/graphql.yml
      ---
      Name: appgraphql
      ---
      SilverStripe\GraphQL\Manager:
        schemas:
          admin:
            scaffolding:
              types:
                SilverStripe\Security\Member:
                  fields: [Email]
                  operations:
                    read: true
      

      请注意,我还为此添加了operations.read: true,因为CMS 将只允许您通过readOne 操作一次读取一个成员。对于您的情况,您需要启用read,它会返回一个分页列表。 More info on available operations.

      测试您的查询

      最简单的方法是安装 GraphiQL (via silverstripe-graphql-devtools),这是一个基于 Web(或应用)的 UI,用于检查 GraphQL 模式并针对您的服务器运行查询。这可以通过 Composer 轻松完成:

      composer require --dev silverstripe/graphql-devtools dev-master
      

      打开浏览器到http://localhost/dev/graphiql?flush。将 localhost 替换为运行 SilverStripe 服务器的任何内容。您将 ?flush 添加到查询字符串中,以告诉 SilverStripe 刷新其缓存(YAML 和 PHP 文件)以获取您的新模块和配置。

      当您获得 GraphiQL 查询编辑器时,您可以先写 query GetUsers { ... },然后您会注意到,当您在查询中输入更深的内容时,它会自动为您完成可用选项。

      这是检索您的会员电子邮件地址的查询:

      query GetUserEmails {
        readSilverStripeMembers {
          edges {
            node {
              Email
            }
          }
        }
      }
      

      微观解释:GetUserEmails 是您创建的任意查询名称。你实际上不需要写一个,没有它也能正常工作。 readSilverStripeMembers 是一个自动构建的查询名称,这是因为您在 GraphQL 操作中启用了 read: true。如果您删除它并再次开始输入,您还会看到其他可用选项,CMS 开箱即用的选项是readOneSilverStripeMemberedgesnode 级别用于分页。

      使用查询

      在我看来,您的 SilverStripe 服务器已经在某个地方运行,您可能没有要测试的本地版本。如果是这种情况,只需将上面的 YAML 配置添加到您的 app 文件夹并部署它就足以让您的服务器在管理员 GraphQL 调用中向成员发送电子邮件,然后您可以使用 cURL 或其他方式进行 GraphQL 查询:

      curl 'http://localhost/admin/graphql/' \       # Adjust to your domain
        -H 'Authorization: Basic YWRtaW46YWRtaW4=' \ # This is admin:admin base64 encoded
        -H 'Content-Type: application/json' \        # Required for the input data structure
        --data-binary '{"query":"query { readSilverStripeMembers { edges { node { Email } } } }","variables":null}'
      

      示例输出:

      {
        "data": {
          "readSilverStripeMembers": {
            "edges": [
              {
                "node": {
                  "Email": "leslie.lawless@example.com"
                }
              },
              {
                "node": {
                  "Email": "mika@example.com"
                }
              },
              {
                "node": {
                  "Email": "sam@example.com"
                }
              }
            ]
          }
        }
      }
      

      【讨论】:

      • 感谢您的全面回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 2020-12-01
      • 2014-12-31
      相关资源
      最近更新 更多