0

I am currently developing a Facebook App in which I wish to add Uservoice forums and suggestions

I have managed to use the API to pull in the forums and suggestions that have already been created, but I am now wanting to allow the user to create / vote on suggestions within a forum. The documentation at UserVoice does not give examples of setting an app in PHP with Oauth.

I am new the OAuth topic and have done some reading around the subject and understand the fundamentals of how OAuth works but I just dont know how to implement the requests in PHP. Any help would be appreciated

Thanks

James
  • 39
  • 1
  • 10

1 Answers1

2

We have just recently worked on a new UserVoice PHP library. The following example finds the first forum in your UserVoice account, posts a new suggestion as user@example.com and then updates user vote count to 2 in the same suggestion using the library:

<?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");
    }
?>

Be sure to include uservoice/uservoice in your composer.json. If you don't use Composer, just clone the project from GitHub.

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

You need OAuth and mcrypt PHP5 extensions.

More examples and installation instructions: http://developer.uservoice.com/docs/api/php-sdk/

raimo
  • 76
  • 5