Please help us help you and fill where relevant:
Your LimeSurvey version: LimeSurvey Community Edition Version 6.16.10+260223
Own server or LimeSurvey hosting: Own Server
Survey theme/template: Fruity TwentyThree ( fruity_twentythree )
==================
Has anyone gotten the API to work using JSON/PHP to add a participant to a survey?
My GLOBAL SETTINGS ---> INTERFACES are set as follows (Personally Identifiable Information redacted and indicated as
BOLD and
STRIKETHROUGH)::
RPC interface enabled: JSON-RPC
URL: https://
NONEYABUISINESS/limesurvey/index.php?r=admin/remotecontrol
Publish API on /admin/remotecontrol: ON
Set Access-Control-Allow-Origin header: ON
I am attempting to add the participant through the following code (Personally Identifiable Information redacted and indicated as
BOLD and
STRIKETHROUGH):
Code:
/* ---------------------------------------------------------
JSON-RPC CLIENT FOR LIMESURVEY API
--------------------------------------------------------- */
class jsonRPCClient {
private $url;
public function __construct($url) {
$this->url = $url;
}
public function __call($method, $params) {
$request = json_encode([
'method' => $method,
'params' => $params,
'id' => 1
]);
$context = stream_context_create([
'http' => [
'method' => "POST",
'header' => "Content-Type: application/json\r\n" .
"User-Agent: PHP-JSONRPC-Client\r\n" .
"Accept: application/json\r\n",
'content' => $request,
'ignore_errors' => true
]
]);
$response = file_get_contents($this->url, false, $context);
if ($response === false) {
error_log("HTTP Response Headers: " . print_r($http_response_header, true));
}
return json_decode($response, true);
}
}
and
Code:
/* ---------------------------------------------------------
FUNCTION: ADD PARTICIPANT TO LIMESURVEY
--------------------------------------------------------- */
function addToLimeSurveyCPDB($first, $last, $email) {
$apiUrl = "https://[s][b]NONEYABUISINESS[/b][/s]/limesurvey/index.php?r=admin/remotecontrol";
$apiUser = "[s][b]ADMIN_USER[/b][/s]";
$apiPass = "[s][b]PASSWORD[/b][/s]";
// Your CPDB feeder survey ID
$surveyId = [s][b]XXXXXX[/b][/s];
$client = new jsonRPCClient($apiUrl);
// 1. Start session
$sessionResponse = $client->get_session_key($apiUser, $apiPass);
$session = $sessionResponse['result'];
if (!$session) {
error_log("LimeSurvey API: Failed to obtain session key");
return false;
}
// 2. Prepare participant data
$participantData = [
[
"firstname" => $first,
"lastname" => $last,
"email" => $email,
"language" => "en",
"emailstatus" => "OK"
]
];
// 3. Add participant to SURVEY
$result = $client->add_participants($session, $surveyId, $participantData, 1);
error_log("CPDB API Response: " . print_r($result, true));
// 4. End session
$client->release_session_key($session);
// 5. Return new participant ID
if (isset($result['result'][0]['participant_id']) && $result['result'][0]['participant_id'] !== "") {
return $result['result'][0]['participant_id'];
}
error_log("LimeSurvey API: Failed to add participant: " . print_r($result, true));
return false;
}
And finally:
Code:
/* ---------------------------------------------------------
ADD VERIFIED USER TO LIMESURVEY
--------------------------------------------------------- */
$participantId = addToLimeSurveyCPDB($first, $last, $email);
if ($participantId === false) {
error_log("CPDB Sync Failed for contractor ID $id ($email)");
} else {
error_log("CPDB Sync Success: Participant ID $participantId for contractor ID $id");
}
The survey I am trying to insert them into is ACTIVE.
ANY guidance would be appreciated!