将OAuth与UserVoice API一起使用

时间:2012-02-21 11:56:54

标签: php uservoice

我目前正在开发一个Facebook应用程序,我希望在其中添加Uservoice论坛和建议

我已设法使用API​​来引入已创建的论坛和建议,但我现在想要允许用户在论坛中创建/投票。 UserVoice的文档没有给出使用Oauth在PHP中设置应用程序的示例。

我是OAuth主题的新手,已经围绕这个主题做了一些阅读,并了解了OAuth如何工作的基础知识,但我不知道如何在PHP中实现请求。任何帮助将不胜感激

由于

1 个答案:

答案 0 :(得分:2)

我们刚刚开发了一个新的UserVoice PHP library。以下示例查找UserVoice帐户中的第一个论坛,将新建议发布为user@example.com,然后使用该库在同一建议中将用户投票计数更新为2:

<?php
    require_once('vendor/autoload.php');
    try {
        // Create a new UserVoice client for subdomain.uservoice.com
        $client = new \UserVoice\Client('subdomain', 'API KEY', 'API SECRET');

        // Get access token for user@example.com
        $token = $client->login_as('user@example.com');

        // Get the first accessible forum's id
        $forums = $token->get_collection('/api/v1/forums', array('limit' => 1));
        $forum_id = $forums[0]['id'];

        $result = $token->post("/api/v1/forums/$forum_id/suggestions", array(
                'suggestion' => array(
                    'title' => 'Move the green navbar to right',
                    'text' => 'Much better place for the green navbar',
                    'votes' => 1
                )
        ));
        $suggestion_id = $result['suggestion']['id'];
        $suggestion_url = $result['suggestion']['url'];

        print("See the suggestion at: $suggestion_url\n");

        // Change to two instead of one votes.
        $token->post("/api/v1/forums/$forum_id/suggestions/$suggestion_id/votes",
            array('to' => 2 )
        );
    } catch (\UserVoice\APIError $e) {
        print("Error: $e\n");
    }
?>

请务必在composer.json中包含uservoice / uservoice。如果您不使用Composer,只需从GitHub克隆项目。

"require": {
    "uservoice/uservoice": "0.0.6"
}

您需要OAuthmcrypt PHP5扩展程序。

更多示例和安装说明:http://developer.uservoice.com/docs/api/php-sdk/