Code:
<?php
define( 'LS_BASEURL', 'https://pathTo/limesurvey/');
define( 'LS_USER', 'admin' );
define( 'LS_PASSWORD', '****' );
require_once 'relativePathTo/application/libraries/jsonRPCClient.php';
$iSurveyID = 885886;
$token = 'ABCDE';
$email = 'example@example.com';
$LastNameAPI = 'Firstname' ;
$FirstNameAPI = 'Lastname' ;
// 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 );
// Define the token params
$tokenParams = array("email"=>$email,"lastname"=>$LastNameAPI,"firstname"=>$FirstNameAPI,"token"=>$token,"language"=>'en',"emailstatus"=>"OK");
$aParticipantData=array($tokenParams);
$bCreateToken = false;
// Create the tokens
$newToken = $myJSONRPCClient->add_participants( $sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken);
// Print returned results
echo '<hr><br><h1>Limesurvey</h1><br>New token created in survey '.$iSurveyID.':'
.'<ul>'
.'<li>TID - '.$newToken[0]['tid'].'</li>'
.'<li>Token - '.$newToken[0]['token'].'</li>'
.'</ul>';
// Send an invitation to the created token
$tokenIDs = array($newToken[0]['tid']);
$newMail = $myJSONRPCClient->invite_participants($sSessionKey, $iSurveyID, $tokenIDs, true);
// Print returned results
if($newMail[$newToken[0]['tid']]['status'] == 'OK') {
echo 'Invitation sent to:'
.'<ul>'
.'<li>Name - '.$newMail[$newToken[0]['tid']]['name'].'</li>'
.'<li>Email - '.$newMail[$newToken[0]['tid']]['email'].'</li>'
.'</ul>';
}
else {
echo 'Error - no invitation sent!';
}
// Release the session key
$myJSONRPCClient->release_session_key( $sSessionKey );
?>
If you want to generate a random token value instead of defining it, change this: