Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Create token on the fly

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago #168808 by Joffm
Replied by Joffm on topic Create token on the fly
1. Did you include the jsonRPCClient by composer?

// 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';


Otherwise you have to uncomment the "require_once" and set the correct path.
And comment the "vendor/autoload.php"

2. I think this line
Code:
$myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient(LS_BASEURL . '/admin/remotecontrol');
should be
Code:
$myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient(LS_BASEURL . '/index.php/admin/remotecontrol');

Try and echo this line to see if it is correct.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 11 months ago - 5 years 11 months ago #168815 by tpartner
Replied by tpartner on topic Create token on the fly
Here is a very simple example that will return the properties of survey in LS 3.x. In this case, the jsonRPCClient.php file resides in the same directory as the PHP script below.

Code:
<?php
 
  require_once 'jsonRPCClient.php';
 
  define( 'LS_BASEURL', 'http://pathTo/LimeSurvey');  
  define( 'LS_USER', 'admin' );
  define( 'LS_PASSWORD', 'password' );
 
  //$iSurveyID = $_GET['surveyID'];
  $iSurveyID = 885886;
 
 
  if(ctype_alnum($iSurveyID) &amp;&amp; (strlen($iSurveyID) == 5 || strlen($iSurveyID) == 6)) { // Valid SID format
 
    // 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 );
 
    if(is_array($sSessionKey)) { // Invalid session
      echo $sSessionKey['status'];
    }
    else if($sSessionKey) { // Valid session
 
      $surveyProperties = $myJSONRPCClient->get_survey_properties($sSessionKey,  $iSurveyID);
 
      foreach($surveyProperties as $key => $value) {
        echo $key . ' => ' . $value . '<br />';
      }
    }
 
    // Release the session key
    $myJSONRPCClient->release_session_key( $sSessionKey );
  }
  else { // Invalid SID format
    die( 'Invalid format!' );
  }
 
?>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 11 months ago by tpartner.
The topic has been locked.
More
5 years 11 months ago #168833 by jinformatique
Replied by jinformatique on topic Create token on the fly
Thank you for your help. I forgot to mention that I did include the jsonRPCClient by composer.
Now I copied the file JsonRPCClient.php in the same directory.


My php file from where I do the calling is on my local computer with PHP 7.1
The limesurvey install is on a hosting with PHP 5.6. Can the error come from this setup?

I took the code from tpartner and changed the connexion values and added a var_dump.

Code:
<?php
 
require_once 'JsonRPCClient.php';
 
define('LS_BASEURL', 'https://www.fnak.fr/limesurvey');  // adjust this one to your actual LimeSurvey URL
define('LS_USER', 'user');
define('LS_PASSWORD', 'password');
 
//$iSurveyID = $_GET['surveyID'];
$iSurveyID = 616685;
 
echo '<pre>';
if (ctype_alnum($iSurveyID) &amp;&amp; (strlen($iSurveyID) == 5 || strlen($iSurveyID) == 6)) { // Valid SID format
    // Instantiate a new RPC client
    $myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient(LS_BASEURL . '/index.php/admin/remotecontrol');
    var_dump($myJSONRPCClient);
 
    // Get a session key
    $sSessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD);
 
    if (is_array($sSessionKey)) { // Invalid session
        echo $sSessionKey['status'];
    } elseif ($sSessionKey) { // Valid session
        $surveyProperties = $myJSONRPCClient->get_survey_properties($sSessionKey, $iSurveyID);
 
        foreach ($surveyProperties as $key => $value) {
            echo $key . ' => ' . $value . '<br />';
        }
    }
 
    // Release the session key
    $myJSONRPCClient->release_session_key($sSessionKey);
} else { // Invalid SID format
    die('Invalid format!');
}
echo '</pre>';


So the result is still the same

Code:
object(org\jsonrpcphp\JsonRPCClient)#1 (6) {
  ["debug":"org\jsonrpcphp\JsonRPCClient":private]=>
  bool(false)
  ["url":"org\jsonrpcphp\JsonRPCClient":private]=>
  string(60) "https://www.fnak.fr/limesurvey/index.php/admin/remotecontrol"
  ["id":"org\jsonrpcphp\JsonRPCClient":private]=>
  int(1)
  ["notification":"org\jsonrpcphp\JsonRPCClient":private]=>
  bool(false)
  ["enableCurl":"org\jsonrpcphp\JsonRPCClient":private]=>
  bool(true)
  ["proxy"]=>
  string(0) ""
}


I have to do the new like this:
Code:
new \org\jsonrpcphp\JsonRPCClient

Else if I do the new like this
Code:
new JsonRPCClient(...
I got a blank page. The JsonRPCClient.php from composer is recent and uses the namespace.


Any ideas?
The topic has been locked.
More
5 years 11 months ago #168900 by jinformatique
Replied by jinformatique on topic Create token on the fly
Hi,
So I found out that it's because of the PHP version 5.6 of my hosting.
I installed Limesurvey locally on PHP 7.1 and this way the Json RPC is working fine.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose