Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Add Limesurvey Participant using the API with a supplied custom token

  • vikrantLumiq
  • vikrantLumiq's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 2 months ago #193928 by vikrantLumiq
I am currently using LimeSurvey v3.0.1 and want to add participants to a survey with a token as a parameter through LimeSurvey remote-control 2 API but LimeSurvey is generating a unique token itself every time the API is called.

Request: POST - http://<host>:<port>/index.php/admin/remotecontrol
Request payload:
{
"method": "add_participants",
"params": {
"sSessionKey": "my_session_key",
"iSurveyID": 12345,
"aParticipantData": [
{
"email": "test3@gmail.com",
"lastname": " ",
"firstname": " ",
"language": "en",
"usesleft": 100,
"token": "customToken",
}
],
"bCreateToken": "false"
},
"id": 1
}



Response:
{
"id":1,
"result":[
{
"sent":"N",
"remindersent":"N",
"remindercount":0,
"completed":"N",
"usesleft":100,
"token":"QMnrKZoMoXnZNaz",
"email":"test3@gmail.com",
"lastname":" ",
"firstname":" ",
"language":"en",
"emailstatus":"OK",
"tid":"38",
"participant_id":null,
"blacklisted":null,
"validfrom":null,
"validuntil":null,
"mpid":null
}
],
"error":null
}


Please tell me why this isn't working as expected or if there is somthing wrong with the Request Payload. Thanks!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 2 months ago #193943 by tpartner
I'm not sure why you need to add "id" and maybe you should remove the quotes around "false" when defining the "bCreateToken" paramenter.

This simple PHP example works for me in version 3.22.0:

Code:
<?php
 
  require_once 'pathTo/jsonRPCClient.php';
 
  define( 'LS_BASEURL', 'http://pathTo/limesurvey/');
  define( 'LS_USER', 'admin' );
  define( 'LS_PASSWORD', 'password' );
 
  $iSurveyID = $_GET["sid"];
  $token = $_GET["token"];
  $email = $_GET["email"];
  $firstName =  $_GET["firstname"];
  $lastName = $_GET["lastname"];
  $language = $_GET["lang"];
 
  // Instantiate a new RPC client
  $myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
  // Get a session key
  $sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
  // Check if token already exists
  $tokenExists = false;
  $aConditions = array("completed"=>"N");
  $listParticipants = $myJSONRPCClient->list_participants($sSessionKey, $iSurveyID, 0, 100000, false, false, $aConditions);
  foreach ($listParticipants as $value) {
    if(is_array($value)) {
      if(array_key_exists('token', $value)) {
        if($value['token'] == $token) {
          $tokenExists = true;
        }
      }
    }
  }
 
  // Token does not exist so we'll create one
  if($tokenExists == false) {
 
    // Define the token params
    $tokenParams = array("email"=>$email,"lastname"=>$lastName,"firstname"=>$firstName,"token"=>$token,"language"=>$language,"emailstatus"=>"OK");
    $aParticipantData=array($tokenParams);
    $bCreateToken = false;
 
    // Create the token in survey 2
    $newToken = $myJSONRPCClient->add_participants( $sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken);
 
    // If token successfully created, redirect to survey
    if(isset($newToken[0]['tid']) &amp;&amp; isset($newToken[0]['token'])) {
      echo('New token '.$newToken[0]['token'].', ID '.$newToken[0]['tid']);
    }
    // Otherwise, display errors
    else if (isset($newToken['status'])) {
      echo $newToken['status'];
    }
    else {
      echo '<br />';
      echo 'Error: '.$newToken[0]['errors']['token'][0];
    }
  }
  // Token already exists
  else {
    echo 'The token already exists!';
  }
 
  // Release the session key
  $myJSONRPCClient->release_session_key( $sSessionKey );
?>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose