Welcome to the LimeSurvey Community Forum

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

Using API to export survey results For LS5

  • blocka
  • blocka's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
1 week 1 day ago #267880 by blocka
Please help us help you and fill where relevant:
LimeSurvey version: 5.6.68+240625
Own server or LimeSurvey Cloud: Own
Survey theme/template: N/A
==================
I have a PHP script for LS3 that uses the API to export results from a specific survey. The PHP script worked with LS 3, but doesn't seem to work with LS 5.
I'm wondering if someone might be able to identify where the error lies in the script for LS 5.

The script outputs the <tr></tr> elements, but no results are included. Also the <th></th> group is blank.

Here's the script:
Code:
<?php
 
// Call like https://LSDOMAIN.TLD/api/ExportResponses.php?sid=######&amp;secret=ABCD
 
    require_once 'jsonRPCClient.php';
    
    define( 'LS_BASEURL', 'https://LSDOMAIN.TLD/');  
    define( 'LS_USER', 'admin' );
    define( 'LS_PASSWORD', 'aPasswordBetterThanThis' );
 
     $iSurveyID = $_GET["sid"];;
     $secret = $_GET["secret"];
     
     // echo $secret ;
     
     if ($secret != 'ABCD') {
         die;
     }
 
 
    // Instantiate a new RPC client
    $myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/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, 'all', 'full', 'long', 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 );
 
?>
 

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
1 week 17 hours ago #267893 by DenisChenu
Replied by DenisChenu on topic Using API to export survey results For LS5
When i need to check export, i just do a print_r(base64_decode($surveyResponses), false ); for a json file.

Here : there are a difference in the json export system :

Before : each response are wrapped in an array : then you have array of array of response array.
No : you don't have this wrapped part, then you have array of response array.

Just try $aFirstResponse = reset($aResponses); (unsure on this point but a print_r gove you the information.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team & Official Partner
  • LimeSurvey Community Team & Official Partner
More
1 week 17 hours ago #267894 by DenisChenu
Replied by DenisChenu on topic Using API to export survey results For LS5
The update are here : github.com/LimeSurvey/LimeSurvey/commit/...28e42171ce36eb92a3c1

Move from $aJson[$values[0]] = array_combine($headers, $values); to $aJson = array_combine($headers, $values);

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose