uservoice/uservoice

此包已被废弃且不再维护。未建议替代包。

UserVoice PHP 库用于 API 连接

0.0.10 2017-02-24 14:11 UTC

This package is not auto-updated.

Last update: 2020-01-16 23:14:11 UTC


README

⛔️ [已弃用] 此存储库不再积极维护。您可以分叉项目,但我们不再接受拉取请求或新问题。您可以通过 support@uservoice.com 提出问题。

UserVoice PHP 库用于 API 连接

此库允许您轻松地

  • 生成 SSO 令牌以创建 SSO 用户/登录 UserVoice (http://uservoice.com)。
  • 安全地执行三脚和两脚 UserVoice API 调用,无需担心加密细节(除非您想)。

安装

要安装 OAuth,您应该检查您的操作系统包装系统是否有 PHP OAuth 包可用,以及您的 PHP 版本是否已编译带有 openssl (说明)。例如,对于 PHP 5.4,Homebrew 附带 php54-oauth 包。如果您的包装系统无法为 PHP 安装 OAuth,您只需安装 PECL/PEAR

获取 PEAR 后,您应该有 'pecl' 命令可用,因此运行 (详细说明。Homebrew 等价物:brew install php54-oauth)

sudo pecl install oauth

当您安装了 oauth 后,请在您的 php.ini 文件中指定它们

extension=oauth.so

您还需要 openssl (详细说明)

最后,安装 Composer 并将 composer.phar 放置在您的 PATH 中。在 composer.json 中添加 uservoice/uservoice

"uservoice/uservoice": ">=0.0.5"

然后使用 Composer 安装项目依赖项

composer.phar install

现在您应该可以开始了!

示例

先决条件

  • PHP 必须带有 openssl 编译,并且需要安装 oauth。请参阅上面的安装说明。
  • 将以下配置参数放置在您的应用程序中的某个位置
# Suppose your UserVoice site is at http://uservoice-subdomain.uservoice.com/
$USERVOICE_SUBDOMAIN = 'uservoice-subdomain';
$SSO_KEY = '982c88f2df72572859e8e23423eg87ed'; # Admin Console -> Settings -> General -> User Authentication

# Define an API client at: Admin Console -> Settings -> Channels -> API
$API_KEY = 'oQt2BaunWNuainc8BvZpAm';
$API_SECRET = '3yQMSoXBpAwuK3nYHR0wpY6opE341inL9a2HynGF2';

// Use autoload.php of Composer to use the library and its dependencies:
require_once('vendor/autoload.php');

使用 UserVoice 库生成 SSO 令牌

SSO 令牌可用于为 SSO 用户创建会话。它们能够将用户信息从一套系统同步到另一套系统。通过调用 UserVoice\SSO::generate_sso_token 方法,可以使用 SSO 密钥和给定的 uservoice 子域生成 SSO 令牌,如下所示

<?php
    $sso_token = \UserVoice\SSO::generate_token($USERVOICE_SUBDOMAIN, $SSO_KEY, array(
        'display_name' => "John Doe",
        'email' => 'john.doe@example.com'
    ), 5*60); // the token will be valid for 5 minutes (5*60 seconds) by default

    echo 'https://' . $USERVOICE_SUBDOMAIN . '.uservoice.com/?sso='.$sso_token."\n";
?>

执行 API 调用

您需要创建 UserVoice\Client 的实例。获取 API 客户端的 $API_KEY 和 $API_SECRET,您可以在管理控制台中创建该客户端。转到设置 -> 通道 -> API。

<?

try {
    $client = new \UserVoice\Client($USERVOICE_SUBDOMAIN, $API_KEY, $API_SECRET);

    // Get users of a subdomain (requires trusted client, but no user)
    $users = $client->get_collection("/api/v1/users");

    print "Subdomain \"" . $USERVOICE_SUBDOMAIN . "\" has " . count($users) . " users.\n";

    foreach($users as $user) {
        print("User: \"${user['name']}\", Profile URL: ${user['url']}\n");
    }


    // Now, let's login as mailaddress@example.com, a regular user
    $regular_access_token = $client->login_as('mailaddress@example.com');

    // Example request #1: Get current user.
    $r = $regular_access_token->get("/api/v1/users/current");
    $user = $r['user'];

    print("User: \"${user['name']}\", Profile URL: ${user['url']}\n");

    // Login as account owner
    $owner_access_token = $client->login_as_owner();

    // Example request #2: Create a new private forum limited to only example.com email domain.
    $r = $owner_access_token->post("/api/v1/forums", array(
        'forum' => array(
            'name' => 'PHP Client Private Feedback',
            'private' => true,
            'allow_by_email_domain' => true,
            'allowed_email_domains' => array(
                array('domain' => 'example.com')
            )
        )
    ));
    $forum = $r['forum'];

    print("Forum \"${forum['name']}\" created! URL: ${forum['url']}\n");
} catch (\UserVoice\Unauthorized $e) {
    /* Thrown usually due to faulty tokens, untrusted client or if attempting
     * operations without Admin Privileges
     */
    var_dump($e);
} catch (\UserVoice\NotFound $e) {
    // Thrown when attempting an operation to a resource that does not exist
    var_dump($e);
}

?>

验证 UserVoice 用户

如果您想代表用户进行通话,但又想确保该用户实际上在UserVoice中拥有某些电子邮件地址,您需要使用三脚API调用。只需向用户提供一个授权链接供其点击,这样用户就可以授予您的网站访问其在UserVoice中数据的权限。

<?php

$callback_url = 'https://:3000/'; # your site

$client = new \UserVoice\Client($USERVOICE_SUBDOMAIN, $API_KEY, $API_SECRET, array('callback' => $callback_url));

# At this point you want to print/redirect to client.authorize_url in your application.
# Here we just output them as this is a command-line example.
print("1. Go to " . $client->authorize_url() . " and click \"Allow access\".\n");
print("2. Then type the oauth_verifier which is passed as a GET parameter to the callback URL:\n");

# In a web app we would get the oauth_verifier via a redirection to CALLBACK_URL.
# In this command-line example we just read it from stdin:
$access_token = $client->login_with_verifier(readline());

# All done. Now we can read the current user's email address:
$r = $access_token->get("/api/v1/users/current");
$user = $r['user'];

print("User logged in, Name: ${user['name']}, email: ${user['email']}\n");

?>