- Posts: 1
- Thank you received: 0
Ask the community, share ideas, and connect with other LimeSurvey users!
/* --------------------------------------------------------- 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); } }
/* --------------------------------------------------------- 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; }
/* --------------------------------------------------------- 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"); }
Please Log in to join the conversation.
Please Log in to join the conversation.