Creating Polls Using the Tag API’s

From KickApps Documentation

Jump to: navigation, search

Polls stimulate interaction on your site, grow page views, and collect user data by presenting members with multiple-choice questions, recording their answers, and displaying results.

This tutorial teaches you how to create polls on your site using the KIT Cloud Social Tag API's:

  • On your own hosted page, create a form with a poll question, radio options for each possible response and a submit button. Each radio option in the form should have a unique value which gets passed to the KIT Cloud Social API as a "tag" associated with a unique "mediaId" (one for each poll) that you specify in the API call.
  • When the user submits the poll response, call the Add Tags To Media, Members and Groups API and pass the unique "mediaId" for the poll, as well as a tag associated with that "mediaId."
  • To show the results of the poll call the Retrieve Tag Count API with the unique "mediaId." Your response will look something like this:
 {"tags":{"12345":[{"count":3,"tag":"yes"},{"count":1,"tag":"no"}]},"error":"","status":1,"payload_type":"json"}
    • "tag" is the value that was submitted as the response to the poll.
    • "count" is the number of times the response was submitted

Using this payload, you can calculate the number of times each response was selected.

Note: You can also add polls to your KIT Cloud Social-hosted pages by using a jQuery AJAX call to perform a request to a server-side page that you host. That page can perform the above mentioned logic and then return back a JSON formatted response. This JSON formatted response can then be used to display the poll on your KIT Cloud Social-hosted page.

Contents

Sample Code

Below is some sample PHP code which illustrates how to implement a basic poll on a page hosted on your own servers.

Poll Form

<?php
/*
 * Author: Danny Migliorisi
 * Company: KickApps
 * Description: This component will create a poll application using KickApps APIs
*/


    include("tasks/addTags.php");
    include("tasks/getMedia.php");
    include("tasks/getTagCount.php");
?>

<style type="text/css">
    #container{width:300px;}
    span{display:block; height:30px; text-align:center;}
    .wrapper{height:30px; line-height:30px; width:100px; border:1px solid #333; float:left;}
</style>

<div id="container">
    <?php

    // If the form has been submitted, call the addTags function
    if(isset($_POST['submit'])) {
        echo "I voted: <strong>".$_POST['tag']."</strong><br />";
        addTags($_POST['asid'], $_POST['type'], $_POST['mediaId'], $_POST['token'], $_POST['tag']);
    }

    // Define variables to be used as parameters for the getMedia function
    // These can be dynamic variables, but for now we will just use some default values
    $asid = '155438';
    $mediaType = 'photo';
    $mediaId = '9931803';

    //Execute the getMedia function
    getMedia($asid, $mediaType, $mediaId);
    ?>
</div>

Add Tags

<?php
/*
 * Author: Danny Migliorisi
 * Company: KickApps
 * Description: Adds a tag to a media item using REST API call.
*/

function addTags($asid, $mediaType, $mediaId, $token, $tag) {

        $rest_url = "http://api.kickapps.com/rest/tags/add/".$mediaType."/".$mediaId."/".$asid;
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $rest_url);
        curl_setopt ($c, CURLOPT_POST, 1);
        curl_setopt ($c, CURLOPT_POSTFIELDS, 't='.$token.'&tags='.$tag);
        curl_setopt($c, CURLOPT_HEADER, false);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec ($c);
        curl_close ($c);

    }
?>

Calculating Results

<?php
/* 
 * Author: Danny Migliorisi
 * Company: KickApps
 * Description: Retrieve tag count for media item.
*/

function getTagCount($asid, $mediaId, $token, $mediaType) {

    // Execute the API call using cURL
    $rest_url = "http://api.kickapps.com/rest/tags/count/".$mediaType."/".$asid;
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, $rest_url);
    curl_setopt ($c, CURLOPT_POST, 1);
    curl_setopt ($c, CURLOPT_POSTFIELDS, 't='.$token.'&mediaId='.$mediaId);
    curl_setopt($c, CURLOPT_HEADER, false);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec ($c);
    curl_close ($c);

    //Decode JSON
    $response = json_decode($response);

    //Set $totalCount to 0
    $totalCount=0;

    //loop through the array of tag counts and collect $totalCount
    foreach($response->tags->{$mediaId} as $key => $value) {
        //increment $totalCount for later use
        $totalCount += $value->count;
    }

    //Loop through the array of tag counts and display the count per tag.
    foreach($response->tags->{$mediaId} as $key => $value) {
        if($value->tag == "yes") {
            //calculate the percentage of 'yes' votes.
            $width=(($value->count)/($totalCount/100));

            //display the yes votes
            echo "<div class='wrapper'>
                    <span class='yes' style='background:blue; color:white; width:".$width."%'>Yes</span>
                  </div>
                  <div style='float:right'>
                        (".(int)$width."%)
                  </div>
                  <div style='clear:both'></div>";
        } else if($value->tag == "no") {
            //calculate the percentage of 'no' votes.
            $width=(($value->count)/($totalCount/100));

            //display the no votes
            echo "<div class='wrapper'>
                    <span class='no' style='background:red; color:white; width:".$width."%'>No</span>
                  </div>
                  <div style='float:right'>
                    (".(int)$width."%)
                  </div>
                  <div style='clear:both'></div>";
        }
    }
    //Display the $totalCount
    echo "Total votes:".$totalCount;
}
?>

List of Kit Cloud Social API Calls


Note: Occasionally (and without advanced warning) we modify our API calls by adding new parameters in the response payload. Please keep this in mind when designing your strategy for parsing our responses.