Add Limesurvey Participant using the API with a supplied custom token

Plus d'informations
il y a 1 mois 2 semaines #193928 par 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": "Cette adresse e-mail est protégée contre les robots spammeurs. Vous devez activer le JavaScript pour la visualiser.",
"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":"Cette adresse e-mail est protégée contre les robots spammeurs. Vous devez activer le JavaScript pour la visualiser.",
"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!

Connexion ou Créer un compte pour participer à la conversation.

LimeSurvey Partners
Plus d'informations
il y a 1 mois 2 semaines #193943 par 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:

<?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']) && 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.
Official LimeSurvey Partner - partnersurveys.com

Connexion ou Créer un compte pour participer à la conversation.

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant

Inscrivez-vous à notre Newsletter!