Actions

RemoteControl 2 API: Difference between revisions

From LimeSurvey Manual

No edit summary
(29 intermediate revisions by 11 users not shown)
Line 4: Line 4:
=Introduction=
=Introduction=


LimeSurvey RemoteControl 2 is a XML-RPC/JSON-RPC based web service available in LimeSurvey 2.0 or later which offers '''various [http://api.limesurvey.org/classes/remotecontrol_handle.html API functions]'''. The old [RemoteControl] will not be available any longer in version 2.
LimeSurvey RemoteControl 2 is a XML-RPC/JSON-RPC based web service available in LimeSurvey 2.0 or more recent which offers '''various [https://api.limesurvey.org/classes/remotecontrol_handle.html API functions]'''.  


LSRC2 makes it possible for developers to control specific functionality of LimeSurvey from any other application, without being restricted to PHP as a programming language.
LSRC2 makes it possible for developers to control specific functionality of LimeSurvey from any other application, without being restricted to PHP as a programming language.
Line 27: Line 27:
== How to configure LSRC2==
== How to configure LSRC2==


In a default LimeSurvey installation LSRC2 is disabled. In order to use LSRC2 you must first enable the service, and then adjust the settings to suit your needs. To enable LSRC2 login to the LimeSurvey administration, go to [[Global settings]], choose the tab 'Services' and select one of the two RPC services (XML-RPC or JSON-RPC) service.
 
In a default LimeSurvey installation LSRC2 is disabled. In order to use LSRC2 you must first enable the service, and then adjust the settings to suit your needs. To enable LSRC2 login to the LimeSurvey administration, go to [[Global settings]], choose the tab 'Interfaces' and select one of the two RPC services (XML-RPC or JSON-RPC) service.
 
 
{{Alert|'''Do not forget''' to activate the LimeSurvey API first! To do this, access your global configuration, click on [[Global_settings#Interfaces|Interfaces]] and enable the API setting.}}


== Security==
== Security==
Line 37: Line 41:
The basic LSRC2 URL is: http://<your_domain>/<your_limesurvey_dir>/index.php/admin/remotecontrol
The basic LSRC2 URL is: http://<your_domain>/<your_limesurvey_dir>/index.php/admin/remotecontrol


LSRC2 fully complies to the [http://www.xmlrpc.com/ XML-RPC specification] and JSON-RPC version 1 specifications. We recommend in general to use JSON-RPC because it is well tested and has a much smaller footprint than XML-RPC.
LSRC2 fully complies to the [http://www.xmlrpc.com/ XML-RPC specification] and [https://www.jsonrpc.org/specification_v1 JSON-RPC version 1] specifications. We recommend in general to use JSON-RPC because it is well tested and has a much smaller footprint than XML-RPC.


LSRC2 offers a lot of functions. Please check the automatically generated [http://api.limesurvey.org/classes/remotecontrol_handle.html API Documentation].
LSRC2 offers a lot of functions. Please check the automatically generated [http://api.limesurvey.org/classes/remotecontrol_handle.html API Documentation].
Line 49: Line 53:


<source lang=php>
<source lang=php>
function get_session_key(string $username, string $password)
function get_session_key(string $username, string $password, string $plugin = 'Authdb')
</source>
</source>
{| class="wikitable"
! Parameter !! Description
|-
| username || The username used for connexion
|-
| password || The password used for connexion
|-
| plugin {{NewIn|3.2.0}} || The Auth plugin to be used
|-
|}


'''Return on success''': (string) session key.
'''Return on success''': (string) session key.
Line 73: Line 88:
'''Return''': Always 'OK' (string)
'''Return''': Always 'OK' (string)


== Other function ==
== Other functions ==


{{Alert|For all other function , you have to refer to the [http://api.limesurvey.org/classes/remotecontrol_handle.html online API ] . If your LimeSurvey version is outdated, you can publish your API and look at it directly.}}
{{Alert|For all other function , you have to refer to the [http://api.limesurvey.org/classes/remotecontrol_handle.html online API ] . If your LimeSurvey version is outdated, you can publish your API and look at it directly.}}
{{Alert|Parameter structure use array of values, you must send the values in the API expected order (see [https://www.jsonrpc.org/specification_v1#a1.1Requestmethodinvocation jsonrpc  specification]).}}


= Example and helper =
= Example and helper =
Line 81: Line 98:
== PHP Example ==
== PHP Example ==


To include JSON-RPC in your application, you can write an application using the light-weight jsonRPCClient from the [https://github.com/weberhofer/jsonrpcphp jsonrpcphp Github repository]. The library can also get included by composer calling
To include JSON-RPC in your application, you can write an application using the light-weight jsonRPCClient from the [https://github.com/weberhofer/jsonrpcphp jsonrpcphp Github repository]. The library can also get included [https://getcomposer.org/download/ by composer calling]


<source lang=bash>
<source lang=bash>
Line 97: Line 114:
</source>
</source>


This is a example how to connect to limesurvey:
This is an example how to connect to limesurvey:


<source lang="php">
<source lang="php">
Line 107: Line 124:
include_once 'vendor/autoload.php';
include_once 'vendor/autoload.php';


define( 'LS_BASEURL', 'http://localhost/limesurvey/');  // adjust this one to your actual LimeSurvey URL
define( 'LS_BASEURL', 'https://localhost/limesurvey/');  // adjust this one to your actual LimeSurvey URL
define( 'LS_USER', 'rpcuser' );
define( 'LS_USER', 'rpcuser' );
define( 'LS_PASSWORD', 'mypassword' );
define( 'LS_PASSWORD', 'mypassword' );
Line 114: Line 131:
$survey_id=374699;
$survey_id=374699;


// instanciate a new client
// instantiate a new client
$myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );
$myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );


Line 120: Line 137:
$sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
$sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );


// receive all ids and info of groups belonging to a given survey
// receive surveys list current user can read
$groups = $myJSONRPCClient->list_groups( $sessionKey, $survey_id );
$groups = $myJSONRPCClient->list_surveys( $sessionKey );
print_r($groups, null );
print_r($groups, null );


Line 128: Line 145:


</source>
</source>
See a [https://gitlab.com/SondagesPro/coreAndTools/twigExtendByPlugins/snippets/1729193 snippet example of list_surveys]


== JAVA example ==
== JAVA example ==
Line 152: Line 171:
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject  jobject = jelement.getAsJsonObject();
JsonObject  jobject = jelement.getAsJsonObject();
String result = jobject.get("result").toString();
String result = jobject.get("result").getAsString();
return result;
return result;
}
}
Line 160: Line 179:
       DefaultHttpClient client = new DefaultHttpClient();           
       DefaultHttpClient client = new DefaultHttpClient();           
          
          
       HttpPost post = new HttpPost("http://PATH_OF_YOUR_SERVER/index.php/admin/remotecontrol");
       HttpPost post = new HttpPost("https://PATH_OF_YOUR_SERVER/index.php/admin/remotecontrol");
       post.setHeader("Content-type", "application/json");
       post.setHeader("Content-type", "application/json");
       post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": {\"username\": \"YOUR_USERNAME\", \"password\": \"YOUR_PASSWORD\" }, \"id\": 1}"));
       post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": [\"YOUR_USERNAME\", \"YOUR_PASSWORD\" ], \"id\": 1}"));
       try {
       try {
         HttpResponse response = client.execute(post);
         HttpResponse response = client.execute(post);
Line 168: Line 187:
             HttpEntity entity = response.getEntity();
             HttpEntity entity = response.getEntity();
             String sessionKey = parse(EntityUtils.toString(entity));
             String sessionKey = parse(EntityUtils.toString(entity));
             post.setEntity( new StringEntity("{\"method\": \"list_groups\", \"params\": {\"sSessionKey \": "+sessionKey+", \"iSurveyID \": \"ID_SURVEY\" }, \"id\": 1}"));
             post.setEntity( new StringEntity("{\"method\": \"list_groups\", \"params\": [ \""+sessionKey+"\", \"ID_SURVEY\" ], \"id\": 1}"));
             response = client.execute(post);
             response = client.execute(post);
             if(response.getStatusLine().getStatusCode() == 200){
             if(response.getStatusLine().getStatusCode() == 200){
Line 187: Line 206:
== Python example and glue ==
== Python example and glue ==


* [https://github.com/TaiSHiNet/lime-py-api LimeSurvey API - Python 3 glue]
* [https://github.com/TaiSHiNet/lime-py-api LimeSurvey API - Python 2 glue]
* [https://github.com/lindsay-stevens/limesurveyrc2api LimeSurvey API - Basic client library in Python 3]. Includes tests. Listed on Pypi: pip install limesurveyrc2api
 
The following code runs with Python 2 and requires some adaptation to work with Python 3.


<syntaxhighlight lang="python" enclose="div">
<syntaxhighlight lang="python" enclose="div">
Line 199: Line 221:
          
          
def get_session_key():
def get_session_key():
     req = urllib2.Request(url='http://myurl/index.php/admin/remotecontrol',\
     req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                           data='{\"method\":\"get_session_key\",\"params\":{\"username\":\"admin\",\"password\":\"mypassword\"},\"id\":1}')
                           data='{\"method\":\"get_session_key\",\"params\":[\"admin\",\"mypassword\"],\"id\":1}')
     req.add_header('content-type', 'application/json')
     req.add_header('content-type', 'application/json')
     req.add_header('connection', 'Keep-Alive')
     req.add_header('connection', 'Keep-Alive')
Line 214: Line 236:
def get_question_properties(skey,QuestionID):
def get_question_properties(skey,QuestionID):
     req = urllib2.Request(url='http://myurl/index.php/admin/remotecontrol',\
     req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                           data='{\"method\":\"get_question_properties\",\"params\":{\"sSessionKey\":\"'+skey+'\",\"iQuestionID\":'+QuestionID+',\
                           data='{\"method\":\"get_question_properties\",\"params\":[\"'+skey+'\",'+QuestionID
\"aQuestionSettings\":[\"gid\",\"type\",\"help\",\"language\",\"sid\",\"question_order\",\"question\",\"subquestions\"]},\"id\": 1}')
                                    +',[\"gid\",\"type\",\"help\",\"language\",\"sid\",\"question_order\",\"question\",\"subquestions\"]],\"id\": 1}')


     req.add_header('content-type', 'application/json')
     req.add_header('content-type', 'application/json')
Line 232: Line 254:
def release_session_key(relkey):
def release_session_key(relkey):
     req = urllib2.Request(url='http://myurl/index.php/admin/remotecontrol',\
     req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                           data='{\"method\":\"release_session_key\",\"params\":{\"sSessionKey\":\"'+relkey+'\"},\"id\":1}')
                           data='{\"method\":\"release_session_key\",\"params\":[\"'+relkey+'\"],\"id\":1}')
     req.add_header('content-type', 'application/json')
     req.add_header('content-type', 'application/json')
     req.add_header('connection', 'Keep-Alive')
     req.add_header('connection', 'Keep-Alive')
Line 248: Line 270:


def export_responses2(skey,sid):
def export_responses2(skey,sid):
     req = urllib2.Request(url='http://myurl/index.php/admin/remotecontrol',\
     req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                           data='{\"method\":\"export_responses\",\"params\":{\"sSessionKey\":\"'+skey+'\",\"iSurveyID\":\"'+sid+'\",\
                           data='{\"method\":\"export_responses\",\"params\":[\"'+skey+'\",\"'+sid+'\",\"csv\",\"de\",\"full\"],\
\"DocumentType\":\"csv\",\"sLanguageCode\":\"de\",\"sHeadingType\":\"full\"},\
"id\": 1}')
"id\": 1}')
     req.add_header('content-type', 'application/json')
     req.add_header('content-type', 'application/json')
Line 286: Line 307:
var SESSIONKEY="";
var SESSIONKEY="";
var options = {
var options = {
   url: "http://xxxxxxxxxxxxxxxx/index.php/admin/remotecontrol",
   url: "https://xxxxxxxxxxxxxxxx/index.php/admin/remotecontrol",
   method: "POST",
   method: "POST",
   headers: {
   headers: {
Line 298: Line 319:


//*******AUTHENTIFICATION*******
//*******AUTHENTIFICATION*******
options.body = JSON.stringify({method:'get_session_key',params:{username:'xxxxx',password:'xxxxxx'},id:1});
options.body = JSON.stringify({method:'get_session_key',params:['myusername','mypassword'],id:1});


request(options, function(error, response, body){
request(options, function(error, response, body){
Line 318: Line 339:
== R example and helper ==
== R example and helper ==


https://github.com/cloudyr/limer
The easiest way to use lsrc2 is by means of the limer package, See https://github.com/cloudyr/limer


Example of usage of this client
Example of usage of this client
<source lang=R>
<source lang=R>
# first limer must be installed
# first limer (check version: must be recent) must be installed
# If you use a version of limer of before june 2016: update it, the output is changed since then
if(!require("devtools")) {
if(!require("devtools")) {
   install.packages("devtools")
   install.packages("devtools")
Line 335: Line 355:
#change the next options (website, user, password)
#change the next options (website, user, password)
options
options
(lime_api = 'http://www.XXX.nl/index.php/admin/remotecontrol')
(lime_api = 'https://www.XXX.nl/index.php/admin/remotecontrol')
options(lime_username = 'user')
options(lime_username = 'user')
options(lime_password = 'password')
options(lime_password = 'password')
Line 344: Line 364:
get_session_key()
get_session_key()


# list all surveys. A vector is returned
# list all surveys. A dataframe is returned
survey_vector<-call_limer(method='list_surveys')
survey_df<-call_limer(method='list_surveys')
 
print(survey_df)
print(survey_matrix[1:2,])
#    sid          surveyls_title startdate            expires active
#    sid          surveyls_title startdate            expires active
#1  999999  XXXX              NA 2016-03-08 15:20:30      Y
#1  999999  XXXX              NA 2016-03-08 15:20:30      Y
#2  999998  XXXX              NA  <NA>      Y
#2  999998  XXXX              NA  <NA>      Y


#Read the data if the first survey (sid=999999) into a data.frame.  
#Read the data of the first survey (sid=999999) into a data.frame.  
#Notice that the default sLanguageCode = en, so maybe you have to  
#Notice that the default sLanguageCode = en, so maybe you have to  
#specify another language (here: dutch)
#specify another language (here: All languages)
data<- get_responses(iSurveyID= 999999, sLanguageCode= '', sResponseType='short')
       
</source>


data<- get_responses(iSurveyID= 999999, sLanguageCode= 'nl', sResponseType='short')
== C# (Copy & Paste from Forum) ==
          
 
https://www.limesurvey.org/forum/development/99633-c-client-for-ls-remotecontrol2
 
I have written a small json-rpc client in c#. It is a small one but enough for my purposes. You can use it in your wiki as example or maybe it helps here other people.
It is tested and works in my environment but it is possible that i havent found all bugs.
You need from nuget package manager Newtonsoft JSON.
 
RPCclient.cs
<source lang="c#">
using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonRPCclient {
  public class JsonRPCclient {
    private int id = 0;
    /// <summary>
    /// Set JSON-RPC webservice URL
    /// </summary>
    public string URL { set; get; }
    /// <summary>
    /// Set JSON-RPC method
    /// </summary>
    public string Method { set; get; }
    /// <summary>
    /// Add JSON-RPC params
    /// </summary>
    public JObject Parameters { set; get; }
    /// <summary>
    /// Results of the request
    /// </summary>
    public JsonRPCresponse Response { set; get; }
    /// <summary>
    /// Create a new object of RPCclient
    /// </summary>
    public JsonRPCclient() {
      Parameters = new JObject();
      Response = null;
    }
    /// <summary>
    /// Create a new object of RPCclient
    /// </summary>
    /// <param name="URL"></param>
    public JsonRPCclient(string URL) {
      this.URL = URL;
      Parameters = new JObject();
      Response = null;
    }
    /// <summary>
    /// POST the request and returns server response
    /// </summary>
    /// <returns></returns>
    public string Post() {
      try {
        JObject jobject = new JObject();
        jobject.Add(new JProperty("jsonrpc", "2.0"));
        jobject.Add(new JProperty("id", ++id));
        jobject.Add(new JProperty("method", Method));
        jobject.Add(new JProperty("params", Parameters));
        string PostData = JsonConvert.SerializeObject(jobject);
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] bytes = encoding.GetBytes(PostData);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "POST";
        request.ContentType = "application/json";
        request.KeepAlive = true;
        request.ContentLength = bytes.Length;
        Stream writeStream = request.GetRequestStream();
        writeStream.Write(bytes, 0, bytes.Length);
        writeStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
        Response = new JsonRPCresponse();
        Response = JsonConvert.DeserializeObject<JsonRPCresponse>(readStream.ReadToEnd());
        Response.StatusCode = response.StatusCode;
        return Response.ToString();
      }
      catch (Exception e) {
        return e.ToString();
      }
    }
    public void ClearParameters() {
      this.Parameters = new JObject();
    }
  }
  public class JsonRPCresponse {
    public int id { set; get; }
    public object result { set; get; }
    public string error { set; get; }
    public HttpStatusCode StatusCode { set; get; }
    public JsonRPCresponse() { }
    public override string ToString() {
      return "{\"id\":" + id.ToString() + ",\"result\":\"" + result.ToString() + "\",\"error\":" + error + ((String.IsNullOrEmpty(error)) ? "null" : "\"" + error + "\"") + "}";
    }
  }
}
</source>
 
programm.cs
<source lang="c#">
      string Baseurl = "http://localhost/limesurvey/index.php?r=admin/remotecontrol";
      JsonRPCclient client = new JsonRPCclient(Baseurl);
      client.Method = "get_session_key";
      client.Parameters.Add("username", "admin");
      client.Parameters.Add("password", "mypassword");
      client.Post();
      string SessionKey = client.Response.result.ToString();
      client.ClearParameters();
      if(client.Response.StatusCode == System.Net.HttpStatusCode.OK){
         client.Method = "import_survey";
        client.Parameters.Add("sSessionKey", SessionKey);
        client.Parameters.Add("sImportData", Base64Encode(yourImportDataString));
        client.Parameters.Add("sImportDataType", "lss");
        //client.Parameters.Add("sNewSurveyName", "test");
        //client.Parameters.Add("DestSurveyID", 1);
        client.Post();
      }
      client.ClearParameters();
      Console.WriteLine("new survey id:" + client.Response.result.ToString());
      Console.ReadLine();
</source>
</source>


= JSON-RPC notes =
= JSON-RPC notes =


The content-type of the HTTP request must be application/json.  Most formatting errors or a failure to set the content-type header will result in a null response from the server (not a JSON response).  Below is an example of a valid request and response pair.
The content-type of the HTTP request '''must be application/json'''.  Most formatting errors are a failure to set the content-type header.  This will result in a null response from the server (not a JSON response).  Below is an example of a valid request and response pair.


<syntaxhighlight lang="php" enclose="div">Request:
<syntaxhighlight lang="php" enclose="div">Request:
Line 374: Line 544:
   host=mylimesurveyhost.com
   host=mylimesurveyhost.com


   content-length=65
   content-length=67


   user-agent=Apache-HttpClient/4.2.2 (java 1.5)
   user-agent=Apache-HttpClient/4.2.2 (java 1.5)
Line 380: Line 550:
 Post body:
 Post body:


   {"method":"get_session_key","params":{"username":"admin","password":"mypassword"},"id":1}
   {"method":"get_session_key","params":["admin","mypassword"],"id":1}


Response body:
Response body:


 {"id":1,"result":"6htqat38fyr4v7iu72nqgv7xgavkvfcz","error":null}</syntaxhighlight>
 {"id":1,"result":"6htqat38fyr4v7iu72nqgv7xgavkvfcz","error":null}</syntaxhighlight>

Revision as of 10:57, 25 May 2019

Introduction

LimeSurvey RemoteControl 2 is a XML-RPC/JSON-RPC based web service available in LimeSurvey 2.0 or more recent which offers various API functions.

LSRC2 makes it possible for developers to control specific functionality of LimeSurvey from any other application, without being restricted to PHP as a programming language.

The following features are planned:

  • start a predefined survey (change titles and things)
  • add predefined groups or questions
  • activate the survey, restrict it to start and endtime
  • make it closed,
  • add participant data/tokens when you need them
  • return the unused tokens to the main application
  • get a fieldmap for a survey,
  • invite or remind the participants of your survey

...and much more

Requirements

  • libXML installed

Setup

How to configure LSRC2

In a default LimeSurvey installation LSRC2 is disabled. In order to use LSRC2 you must first enable the service, and then adjust the settings to suit your needs. To enable LSRC2 login to the LimeSurvey administration, go to Global settings, choose the tab 'Interfaces' and select one of the two RPC services (XML-RPC or JSON-RPC) service.


  Do not forget to activate the LimeSurvey API first! To do this, access your global configuration, click on Interfaces and enable the API setting.


Security

LSRC2 uses the same security measures as the normal administration login. That means that the permission set of the used username and password is the same as if you would login in the administration with that user/password. Also LSRC2 is protected against brute-force password cracking - like the normal administration login.

How to use LSRC2

The basic LSRC2 URL is: http://<your_domain>/<your_limesurvey_dir>/index.php/admin/remotecontrol

LSRC2 fully complies to the XML-RPC specification and JSON-RPC version 1 specifications. We recommend in general to use JSON-RPC because it is well tested and has a much smaller footprint than XML-RPC.

LSRC2 offers a lot of functions. Please check the automatically generated API Documentation. LSRC2 offers the following functions:

Sessions

get_session_key

Using this function you can create a new XML/JSON-RPC session key. This is mandatory for all following LSRC2 function calls.

function get_session_key(string $username, string $password, string $plugin = 'Authdb')
Parameter Description
username The username used for connexion
password The password used for connexion
plugin (New in 3.2.0 ) The Auth plugin to be used

Return on success: (string) session key.

Return on error: for protocol-level errors (invalid format etc), an error message. For invalid username and password, returns a null error and the result body contains a 'status' name-value pair with the error message.

release_session_key

Using this function you can close a previously opened XML-RPC/JSON-RPC session.

function release_session_key(string $sSessionKey)
Parameter Description
sSessionKey Auth credentials

Return: Always 'OK' (string)

Other functions

  For all other function , you have to refer to the online API . If your LimeSurvey version is outdated, you can publish your API and look at it directly.


  Parameter structure use array of values, you must send the values in the API expected order (see jsonrpc specification).


Example and helper

PHP Example

To include JSON-RPC in your application, you can write an application using the light-weight jsonRPCClient from the jsonrpcphp Github repository. The library can also get included by composer calling

composer require weberhofer/jsonrpcphp

or by inclusion of the following lines in your composer.json file:

{
    "require": {
        "weberhofer/jsonrpcphp": "~2"
    }
}

This is an example how to connect to limesurvey:

<?php

// without composer this line can be used
// require_once 'path/to/your/rpcclient/jsonRPCClient.php';
// with composer support just add the autoloader
include_once 'vendor/autoload.php';

define( 'LS_BASEURL', 'https://localhost/limesurvey/');  // adjust this one to your actual LimeSurvey URL
define( 'LS_USER', 'rpcuser' );
define( 'LS_PASSWORD', 'mypassword' );

// the survey to process
$survey_id=374699;

// instantiate a new client
$myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );

// receive session key
$sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );

// receive surveys list current user can read
$groups = $myJSONRPCClient->list_surveys( $sessionKey );
print_r($groups, null );

// release the session key
$myJSONRPCClient->release_session_key( $sessionKey );

See a snippet example of list_surveys

JAVA example

To decode and code your json calls you can use the library gson as you can see in the following example:

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class TestHttpClient {
	
    public static String parse(String jsonLine) {
	 JsonElement jelement = new JsonParser().parse(jsonLine);
	 JsonObject  jobject = jelement.getAsJsonObject();
	 String result = jobject.get("result").getAsString();
	 return result;
	}
	
	
    public static void main(String[] args) throws UnsupportedEncodingException {
      DefaultHttpClient client = new DefaultHttpClient();          
         
      HttpPost post = new HttpPost("https://PATH_OF_YOUR_SERVER/index.php/admin/remotecontrol");
      post.setHeader("Content-type", "application/json");
      post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": [\"YOUR_USERNAME\", \"YOUR_PASSWORD\" ], \"id\": 1}"));
      try {
        HttpResponse response = client.execute(post);
        if(response.getStatusLine().getStatusCode() == 200){
            HttpEntity entity = response.getEntity();
            String sessionKey = parse(EntityUtils.toString(entity));
            post.setEntity( new StringEntity("{\"method\": \"list_groups\", \"params\": [ \""+sessionKey+"\", \"ID_SURVEY\" ], \"id\": 1}"));
            response = client.execute(post);
            if(response.getStatusLine().getStatusCode() == 200){
                entity = response.getEntity();
                System.out.println(EntityUtils.toString(entity));
                }
           }
       
       
      } catch (IOException e) {
        e.printStackTrace();
      }
    }}

Python example and glue

The following code runs with Python 2 and requires some adaptation to work with Python 3.

import urllib
import urllib2
import json
import sys
# There is an generic json-rpc implemantation in Python but it dose not work for me in this case so I worte Some functions 


        
def get_session_key():
    req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                          data='{\"method\":\"get_session_key\",\"params\":[\"admin\",\"mypassword\"],\"id\":1}')
    req.add_header('content-type', 'application/json')
    req.add_header('connection', 'Keep-Alive')
    try:
        f = urllib2.urlopen(req)
        myretun = f.read()
        #print myretun
        j=json.loads(myretun)
        return j['result']
    except :
        e = sys.exc_info()[0]
        print ( "<p>Error: %s</p>" % e )
		
def get_question_properties(skey,QuestionID):
    req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                          data='{\"method\":\"get_question_properties\",\"params\":[\"'+skey+'\",'+QuestionID
                                    +',[\"gid\",\"type\",\"help\",\"language\",\"sid\",\"question_order\",\"question\",\"subquestions\"]],\"id\": 1}')

    req.add_header('content-type', 'application/json')
    req.add_header('connection', 'Keep-Alive')
    try:
        f = urllib2.urlopen(req)
        myretun = f.read()
        #print myretun
        j=json.loads(myretun)
        return j['result']
    except :
        e = sys.exc_info()[0]
        print ( "<p>Error: %s</p>" % e )

		
def release_session_key(relkey):
    req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                          data='{\"method\":\"release_session_key\",\"params\":[\"'+relkey+'\"],\"id\":1}')
    req.add_header('content-type', 'application/json')
    req.add_header('connection', 'Keep-Alive')
    try:
        f = urllib2.urlopen(req)
        myretun = f.read()
        #print myretun
        j=json.loads(myretun)
        return j['result']
    except :
        e = sys.exc_info()[0]
        print ( "<p>Error: %s</p>" % e )


def export_responses2(skey,sid):
    req = urllib2.Request(url='https://myurl/index.php/admin/remotecontrol',\
                          data='{\"method\":\"export_responses\",\"params\":[\"'+skey+'\",\"'+sid+'\",\"csv\",\"de\",\"full\"],\
"id\": 1}')
    req.add_header('content-type', 'application/json')
    req.add_header('connection', 'Keep-Alive')
    try:
        f = urllib2.urlopen(req)
        myretun = f.read()
        #print myretun
        j=json.loads(myretun)
        return j['result']
    except :
        e = sys.exc_info()[0]
        print ( "<p>Error: %s</p>" % e )		

		
mykey=get_session_key()
print export_responses2(mykey,'566237').decode('base64')
get_question_properties(mykey,'574')
print release_session_key(mykey)

NodeJS example

This script require request, you can include easily with the command

npm install request

This is a example how to connect to limesurvey:

var request = require('request');

//******GLOBAL***************
var SESSIONKEY="";
var options = {
  url: "https://xxxxxxxxxxxxxxxx/index.php/admin/remotecontrol",
  method: "POST",
  headers: {
    'user-agent': 'Apache-HttpClient/4.2.2 (java 1.5)',
    'host': 'xxxxxxxxxxxxxxxx',
    'path': '/index.php/admin/remotecontrol',
    'connection': 'keep-alive',
    'content-type': 'application/json'
  }
};

//*******AUTHENTIFICATION*******
options.body = JSON.stringify({method:'get_session_key',params:['myusername','mypassword'],id:1});

request(options, function(error, response, body){
  if (!error && response.statusCode == 200) {
      body = JSON.parse(body);

    //*********KEEP THE KEY*********  
    if(SESSIONKEY==="") {
      console.log("NEW KEY -->"+body.result);
      SESSIONKEY=body.result;
      nextFonction();
    } 
  }
  else console.log("ERROR -->"+body);  
});

R example and helper

The easiest way to use lsrc2 is by means of the limer package, See https://github.com/cloudyr/limer

Example of usage of this client

# first limer (check version: must be recent) must be installed
if(!require("devtools")) {
  install.packages("devtools")
  library("devtools")
}
install_github("cloudyr/limer")
#############################################################

library(limer)

#change the next options (website, user, password)
options
(lime_api = 'https://www.XXX.nl/index.php/admin/remotecontrol')
options(lime_username = 'user')
options(lime_password = 'password')
#############################################################


# first get a session access key
get_session_key()

# list all surveys. A dataframe is returned
survey_df<-call_limer(method='list_surveys')
print(survey_df)
#    sid           surveyls_title startdate             expires active
#1  999999  XXXX               NA 2016-03-08 15:20:30      Y
#2  999998  XXXX               NA   <NA>      Y

#Read the data of the first survey (sid=999999) into a data.frame. 
#Notice that the default sLanguageCode = en, so maybe you have to 
#specify another language (here: All languages)
data<- get_responses(iSurveyID= 999999, sLanguageCode= '', sResponseType='short')

C# (Copy & Paste from Forum)

https://www.limesurvey.org/forum/development/99633-c-client-for-ls-remotecontrol2

I have written a small json-rpc client in c#. It is a small one but enough for my purposes. You can use it in your wiki as example or maybe it helps here other people. It is tested and works in my environment but it is possible that i havent found all bugs. You need from nuget package manager Newtonsoft JSON.

RPCclient.cs

using System;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace JsonRPCclient {
 
  public class JsonRPCclient {
 
    private int id = 0;
    /// <summary>
    /// Set JSON-RPC webservice URL
    /// </summary>
    public string URL { set; get; }
    /// <summary>
    /// Set JSON-RPC method
    /// </summary>
    public string Method { set; get; }
    /// <summary>
    /// Add JSON-RPC params
    /// </summary>
    public JObject Parameters { set; get; }
 
    /// <summary>
    /// Results of the request
    /// </summary>
    public JsonRPCresponse Response { set; get; }
 
 
    /// <summary>
    /// Create a new object of RPCclient 
    /// </summary>
    public JsonRPCclient() {
      Parameters = new JObject();
      Response = null;
    }
 
    /// <summary>
    /// Create a new object of RPCclient
    /// </summary>
    /// <param name="URL"></param>
    public JsonRPCclient(string URL) {
      this.URL = URL;
      Parameters = new JObject();
      Response = null;
    }
 
    /// <summary>
    /// POST the request and returns server response
    /// </summary>
    /// <returns></returns>
    public string Post() {
      try {
        JObject jobject = new JObject();
        jobject.Add(new JProperty("jsonrpc", "2.0"));
        jobject.Add(new JProperty("id", ++id));
        jobject.Add(new JProperty("method", Method));
        jobject.Add(new JProperty("params", Parameters));
 
        string PostData = JsonConvert.SerializeObject(jobject);
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] bytes = encoding.GetBytes(PostData);
 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "POST";
        request.ContentType = "application/json";
        request.KeepAlive = true;
        request.ContentLength = bytes.Length;
 
        Stream writeStream = request.GetRequestStream();
        writeStream.Write(bytes, 0, bytes.Length);
        writeStream.Close();
 
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
 
        Response = new JsonRPCresponse();
        Response = JsonConvert.DeserializeObject<JsonRPCresponse>(readStream.ReadToEnd());
        Response.StatusCode = response.StatusCode;
 
        return Response.ToString();
      }
      catch (Exception e) {
        return e.ToString();
      }
    }
 
    public void ClearParameters() {
      this.Parameters = new JObject();
    }
 
 
  }
 
  public class JsonRPCresponse {
    public int id { set; get; }
    public object result { set; get; }
    public string error { set; get; }
    public HttpStatusCode StatusCode { set; get; }
 
    public JsonRPCresponse() { }
 
    public override string ToString() {
      return "{\"id\":" + id.ToString() + ",\"result\":\"" + result.ToString() + "\",\"error\":" + error + ((String.IsNullOrEmpty(error)) ? "null" : "\"" + error + "\"") + "}";
    }
  }
 
}

programm.cs

      string Baseurl = "http://localhost/limesurvey/index.php?r=admin/remotecontrol";
      JsonRPCclient client = new JsonRPCclient(Baseurl);
      client.Method = "get_session_key";
      client.Parameters.Add("username", "admin");
      client.Parameters.Add("password", "mypassword");
      client.Post();
      string SessionKey = client.Response.result.ToString();
 
      client.ClearParameters();
 
      if(client.Response.StatusCode == System.Net.HttpStatusCode.OK){
        client.Method = "import_survey";
        client.Parameters.Add("sSessionKey", SessionKey);
        client.Parameters.Add("sImportData", Base64Encode(yourImportDataString));
        client.Parameters.Add("sImportDataType", "lss");
        //client.Parameters.Add("sNewSurveyName", "test");
        //client.Parameters.Add("DestSurveyID", 1);
        client.Post();
      }
 
      client.ClearParameters();
 
      Console.WriteLine("new survey id:" + client.Response.result.ToString());
      Console.ReadLine();


JSON-RPC notes

The content-type of the HTTP request must be application/json.  Most formatting errors are a failure to set the content-type header. This will result in a null response from the server (not a JSON response).  Below is an example of a valid request and response pair.

Request:

 HTTP headers:

   content-type=application/json

   connection=Keep-Alive

   host=mylimesurveyhost.com

   content-length=67

   user-agent=Apache-HttpClient/4.2.2 (java 1.5)

 Post body:

   {"method":"get_session_key","params":["admin","mypassword"],"id":1}

Response body:

 {"id":1,"result":"6htqat38fyr4v7iu72nqgv7xgavkvfcz","error":null}