Welcome to the LimeSurvey Community Forum

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

Display responses from my survey in real time via PHP or HTML

  • h0rr0rOrg
  • h0rr0rOrg's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 10 months ago #184862 by h0rr0rOrg
Hello... I'm a long-time user of LimeSurvey, but a new user in the cloud. I'm trying to display the responses to my survey on our website.

I've looked at the Remote Control 2 API page, and I've activated the API/Json.

I'm new to Json though, and so I'm confused about the instructions on that page. I don't see any API functions that simply grab the data/responses from the survey. Am I missing something?

This is how we have displayed the responses in the past: www.horror.org/awards/2018readinglist.php
We used a direct connection to our SQL database through PHP to display the data.

Any help you can give to get me over the hump to understanding how JSon works with PHP would be greatly appreciated!!

Thanks so much!
Angel...
Horror.org webmaster
The topic has been locked.
  • h0rr0rOrg
  • h0rr0rOrg's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 10 months ago #184863 by h0rr0rOrg
I am stalled right at the beginning. I don't see any indication of how to connect to my Limesurvey other than this:

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

But what is my domain? What is my Limesurvey dir? Where do I find that information? Is this for the cloud or just for an installation?

Thanks!
Angel...
The topic has been locked.
  • h0rr0rOrg
  • h0rr0rOrg's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 10 months ago #184864 by h0rr0rOrg

h0rr0rOrg wrote: I am stalled right at the beginning. I don't see any indication of how to connect to my Limesurvey other than this:

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

But what is my domain? What is my Limesurvey dir? Where do I find that information? Is this for the cloud or just for an installation?

Thanks!
Angel...


Okay, I figured this out.


The instructions on this page ( manual.limesurvey.org/RemoteControl_2_API ) seem to indicate that I need to call my RPC client.

require_once 'path/to/your/rpcclient/jsonRPCClient.php';

Is this RPC on my website server or on the Limesurvey cloud?

Thanks!
Angel...
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 10 months ago #184871 by DenisChenu

h0rr0rOrg wrote: Is this RPC on my website server or on the Limesurvey cloud?

In your website … you can't include a file on another server …

It's clearly noted on manual , no ?
manual.limesurvey.org/RemoteControl_2_API#PHP_Example

To include JSON-RPC in your application, you can write an application using the light-weight jsonRPCClient from the jsonrpcphp Github repository.


Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 10 months ago #184880 by tpartner
Here is a link to a post describing how to use the API in PHP to get a response by token (I couldn't find any examples of the export_responses() method but it will be similar) - www.limesurvey.org/forum/development/118...urvey-rpc-api#182796

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • h0rr0rOrg
  • h0rr0rOrg's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 10 months ago #184890 by h0rr0rOrg
Thanks for all this! I am, however, still fumbling. I have the RPC installed, but I can't figure out how to export the responses. I see a lot of methods I don't need.

By token, do you mean the token assigned a particular respondent or does that also have a different meaning?

My survey does not have tokens. It's not secured in that way.

I just want to pull the data into an array that I can then parse with PHP and display.

Is this possible? Without using tokens?

Thanks!!
Angel...
The topic has been locked.
  • h0rr0rOrg
  • h0rr0rOrg's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 10 months ago #184891 by h0rr0rOrg
I'm getting the following error:
Fatal error: Class 'jsonRPCClient' not found in /home/content/91/8685291/html/members-only/documents/rec-files/new/recs-simple.php on line 27

This is the line it's referencing:
$myJSONRPCClient = new jsonRPCClient( LS_BASEURL,'index.php/admin/remotecontrol' );

And I input my LS_BASEURL like this:
define( 'LS_BASEURL', ' NAME.limesurvey.org/admin/remotecontrol ');
where NAME is the name of my account. I pulled this URL from the Global Settings page. Should I be using the URL of my survey instead or something else?

Thanks for the help!! Troubleshooting sux.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 10 months ago - 4 years 10 months ago #184896 by Joffm
Here the first lines of my php file:
Code:
<?php
 
require_once 'jsonrpcphp/JsonRPCClient.php';
 
define( 'LS_BASEURL', 'https://www.limesurvey.de/survey');  // adjust this one to your actual LimeSurvey URL
define( 'LS_USER', 'user' );
define( 'LS_PASSWORD', 'pass' );
 
 
// the survey to process
$survey_id=557357;
 
// instantiate a new client
$myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
// receive session key
$sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
print_r($sessionKey );
 
if(is_array($sessionKey))
{
    header("Content-type: application/json");
    echo json_encode($sessionKey);
    die();
}
 
/* Get the responses */
$response = $myJSONRPCClient->export_responses(
    $sessionKey,
    $survey_id,
    'json', // Document type : pdf,csv,xls,doc,json
    null, // Language code : null : default from survey
    'all', // Stautus complete|incomplete|all
    null, // Heading : code|full|abbreviated : question text, default code
    null // answer : short|long , default : long
    ); // See http://api.limesurvey.org/classes/remotecontrol_handle.html#method_export_responses or https://github.com/LimeSurvey/LimeSurvey/blob/master/application/helpers/remotecontrol/remotecontrol_handle.php#L2382 or
 

All from the sample in the manual.

You get an array of answers which you download or save to something with your php functions.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 4 years 10 months ago by Joffm.
The following user(s) said Thank You: tpartner
The topic has been locked.
  • h0rr0rOrg
  • h0rr0rOrg's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 10 months ago - 4 years 10 months ago #184905 by h0rr0rOrg
That was really helpful Joffm! Thank you so much!

I've managed to get some of the data displaying, but my foreach is stopping after the first entry. I can't get it to continue on no matter what I do! I've been at this for hours!

Here's the code I'm currently using (after many iterations).
...$responseArr is my full array.
...'responses' is the code name of the array inside it.
...I can manually increment the $i, but it doesn't increment on the fly, and it still always only shows one piece of data.
...I have managed to get it to show one data set (for one submission), but only by echoing the $row2 after it's been reset (which makes its array only include one submission's data)
Code:
  echo '<table border=1 cellpadding=5>';
  $i = 0;
 
foreach ($responseArr as $value)
  {
  $row2 = reset($responseArr['responses'][$i]);
  $title = $row2['title'];
  echo '<tr><td>';
  echo $title . '</td><td><br>' . $i . '</td></tr>';
  ++$i;
  }
  echo '</table>';

I'm at a complete loss. Please, if you have time to help, I'm very grateful.
Last edit: 4 years 10 months ago by h0rr0rOrg. Reason: It hid some of the code. Trying to get it visible.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 10 months ago - 4 years 10 months ago #184946 by tpartner
Here is an example of how to use the export_responses() method in PHP that works for me:

Code:
<?php
 
  require_once 'jsonRPCClient.php';
 
  define( 'LS_BASEURL', 'http://pathTo/limeSurvey');  
  define( 'LS_USER', 'admin' );
  define( 'LS_PASSWORD', 'password' );
 
  $iSurveyID = 123456;
 
  // 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
 
    $surveyResponses = $myJSONRPCClient->export_responses($sSessionKey, $iSurveyID, 'json', null, 'completed', 'code', 'short', null, null, null);
 
    if(is_array($surveyResponses)) {
      // Oops, print any errors
      print_r($surveyResponses);
    }
    else {
      // Decode the retuned base-64 string and convert to an array
      $decodedString = base64_decode($surveyResponses);
      $aResponses = json_decode($decodedString, True);
 
      // Find the first response ID        
      $aFirstResponse = reset($aResponses['responses'][0]);
 
      echo '<table style="border-collapse: collapse; text-align: left;">';    
        echo '<tr>';        
          // Insert column headers
          foreach($aFirstResponse as $key => $value) {
            echo '<th style="border: 1px solid #CCC; padding: 2px 7px;">'.$key .'</th>';
          }
        echo '</tr>';
        foreach($aResponses['responses'] as $key => $row) {
          echo '<tr>';
            // Insert the data
            foreach(reset($row) as $key => $item) {
              echo '<td style="border: 1px solid #CCC; padding: 2px 7px;">'.$item .'</td>';
            }
          echo '</tr>';
        }
      echo '</table>';
    }
  }
 
  // Release the session key
  $myJSONRPCClient->release_session_key( $sSessionKey );
 
?>


Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 10 months ago by tpartner.
The following user(s) said Thank You: h0rr0rOrg
The topic has been locked.
  • h0rr0rOrg
  • h0rr0rOrg's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 9 months ago #184994 by h0rr0rOrg
That is beautiful, Tony! Thanks so much! It got me to the point where I can display all the data like I want.

Now, I'm trying to figure out how to display only specific data.

I have the main array, then "sub-arrays" within it that are the $row.

I want to display only the rows where one of the data points is "poetry".

Then, I only want to display the data related to specific $keys within that row: 'author,' 'title,' etc.

I've tried using nested if statements, and I've tried (in_array()). None of this is working for me.

I suspect it's because of how the foreach is processing the data. I'm asking it to do something too complex.

Does this make any sense? Here's what I want:

if an $item in the $row == 'poetry'
{
echo the row, but only the data in 'author' and 'title';
}

I spent all day (literally) yesterday trying to trial-and-error my way through this and searching the web for a solution. I don't know if I'm too stupid or if I'm just missing a crucial piece of the puzzle.

Thank you to anyone who can help me! Mucho appreciated!
Angel...
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 9 months ago #184995 by Joffm
Hi,
Did you read this?
api.limesurvey.org/classes/remotecontrol_handle.html

I want to display only the rows where one of the data points is "poetry".

Is "poetry" in a well defined column or somewhere in the dataset?

With the "$aFields"-parameter you can select your desired fields.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose