Quiz Web Component - Code Sample
This example shows how you can display the Quiz web component via php code.
Copy
<?php
class Nitro {
/**
* Generate an access token
*
* @param $clientId : OAuth client ID (string | required)
* @param $clientSecret : OAuth client secret (string | required)
* @param $threeLegToken : Generate 3 leg token when true (bool | optional)
* @param $userId : User ID to pass in (string | optional)
*/
public function makeOAuthToken($clientId, $clientSecret, $threeLegToken = false, $userId = ''){
$endpoint = "https://api.sandbox.bunchball.com/oauth/token/";
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$data = "grant_type=client_credentials&client_id=".$clientId."&client_secret=".$clientSecret;
if($threeLegToken){
$data .= "&xoauth_end_user_id:" . $userId;
}
$response = $this->executePost($endpoint, $data, $headers);
return $response;
}
/**
* Execute URL (POST)
*
* @param $url : URL to be passed in (String | Required)>
* @param $data : Post data to send (Array | Required)
* @param $headers : Headers to send with request (Array | Optional)
*/
public function executePost($url, $data, $headers = []){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $this->response = $response;
}
}
?>
Copy
<?php
require_once('nitro.php');
$clientId = 'ggOeXj6Kdo';
$clientSecret = 'MCgaON91T1wmIfUrhlVle';
$userLoginId = 'salesdemo';
$nitro = new Nitro();
//Generate three legged token
$threeLeggedToken = $nitro->makeOAuthToken($clientId, $clientSecret, true, $userLoginId);/p>
if(!empty($threeLeggedToken)) {
//do something like store it in a db or something
//print_r($threeLeggedToken);
} else {
print "Failed fetching access token, response was: " . $nitro->getLastResponse();
}
$authObj = json_decode($threeLeggedToken);
if (isset($authObj->access_token)){
global $accessToken;
$accessToken = $authObj->access_token;
}
//Generate two legged token
//$twoLeggedToken = $nitro->makeOAuthToken($clientId, $clientSecret);
?>
<!DOCTYPE html>
<html xmlns:nitro="http://www.bunchball.com/nitro">
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<div name="My Quiz"
class="nitro-widget"
width="400px"
height="400px"
data-user-id="salesdemo"></div>
<script id="nitro-js"
data-token="<?php echo $accessToken ?>"
data-server="https://api.sandbox.bunchball.com"
data-version="v6.1"
src="https://components.bunchball.com/nitro/v6.1/nitro.min.js"
></script>
</body>
</html>
See also