Latest updates
This commit is contained in:
209
getData.php
209
getData.php
@@ -1,109 +1,152 @@
|
||||
|
||||
<?php
|
||||
// Set up constants:
|
||||
$apiToken="nCqnPnrt9HplICvOWhratTIrwkxqe7pILQ524GJG";
|
||||
$dataCenter = "fra1"; // Example: "us-east-1"
|
||||
$surveyId = "SV_3pyZVUNpxXm1PZI"; //Survey ID
|
||||
$urveyId ="SV_bmiHoSHYWIgGM3I"; //Survey ID
|
||||
//$surveyId = "SV_bmiHoSHYWIgGM3I"; //Survey ID
|
||||
// Settings
|
||||
$config = require 'config.php';
|
||||
require_once 'db.php'; // include the connection
|
||||
$apiToken = $config['api_token'];
|
||||
$dataCentre = $config['data_centre'];
|
||||
$surveyId = $_POST['survey_id'] ?? null;
|
||||
|
||||
// API endpoint URLs
|
||||
$exportUrl = "https://$dataCenter.qualtrics.com/API/v3/surveys/$surveyId/export-responses";
|
||||
$statusUrl = "https://$dataCenter.qualtrics.com/API/v3/surveys/$surveyId/export-responses/";
|
||||
$fileUrl = "https://$dataCenter.qualtrics.com/API/v3/surveys/$surveyId/export-responses/";
|
||||
if (!$surveyId) {
|
||||
$surveyId="SV_bD838sNKZEmi6Tc"; // *****FIXME***** JUST FOR DEVELOPMENT
|
||||
//die("No survey ID provided.");
|
||||
}
|
||||
|
||||
// Step 1: Start Export
|
||||
$baseUrl = "https://$dataCentre.qualtrics.com/API/v3";
|
||||
$headers = [
|
||||
"X-API-TOKEN: $apiToken",
|
||||
"Content-Type: application/json"
|
||||
];
|
||||
|
||||
$ch = curl_init($exportUrl);
|
||||
// Step 1: Start the export (with compress = false)
|
||||
$exportUrl = "$baseUrl/surveys/$surveyId/export-responses/";
|
||||
$postFields = json_encode([
|
||||
"format" => "json",
|
||||
"compress" => false
|
||||
]);
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $exportUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["format" => "json"]));
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
|
||||
$response = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
echo "cURL Error: " . curl_error($ch);
|
||||
curl_close($ch);
|
||||
exit;
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
if (!$response) {
|
||||
die("Error starting export: " . curl_error($ch));
|
||||
$result = json_decode($response, true);
|
||||
echo "<pre>".$surveyId." ".$exportUrl."</pre>";
|
||||
if (!isset($result['result']['progressId'])) {
|
||||
|
||||
echo "<pre>Export API Response:\n";
|
||||
print_r($response);
|
||||
echo "</pre>";
|
||||
die("Failed to start export.\n$response");
|
||||
}
|
||||
$progressId = $result['result']['progressId'];
|
||||
|
||||
echo "Polling for completion";
|
||||
|
||||
|
||||
|
||||
// Step 2: Poll for completion
|
||||
$progressUrl = "$exportUrl/$progressId";
|
||||
do {
|
||||
sleep(2); // avoid hammering the API
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $progressUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
$result = json_decode($response, true);
|
||||
$status = $result['result']['status'] ?? 'failed';
|
||||
} while ($status !== 'complete' && $status !== 'failed');
|
||||
|
||||
if ($status !== 'complete') {
|
||||
die("Export failed or timed out.\n$response");
|
||||
}
|
||||
|
||||
$exportData = json_decode($response, true);
|
||||
$progressId = $exportData['result']['progressId'];
|
||||
$fileId = $result['result']['fileId'];
|
||||
|
||||
// Step 2: Check Export Status
|
||||
$status = 'inProgress';
|
||||
while ($status != 'complete') {
|
||||
// Sleep for 5 seconds before checking again
|
||||
sleep(5);
|
||||
// Step 4: Download the file (uncompressed JSON)
|
||||
$downloadUrl = "$exportUrl/$fileId/file";
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $downloadUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
$response = curl_exec($ch);
|
||||
|
||||
$statusCh = curl_init($statusUrl . $progressId);
|
||||
curl_setopt($statusCh, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($statusCh, CURLOPT_HTTPHEADER, $headers);
|
||||
$statusResponse = curl_exec($statusCh);
|
||||
curl_close($statusCh);
|
||||
if (curl_errno($ch)) {
|
||||
die("Error downloading the file: " . curl_error($ch));
|
||||
}
|
||||
|
||||
if (!$statusResponse) {
|
||||
die("Error checking status: " . curl_error($statusCh));
|
||||
curl_close($ch);
|
||||
|
||||
// Decode the response
|
||||
$data = json_decode($response, true);
|
||||
|
||||
//Save surveyId
|
||||
//insertSurveyIfNotExists($identifier, $description); // The identifier is actually the surveyId, and the description isn't included at this point.
|
||||
$surveyIndex = getOrCreateSurvey($surveyId, "");
|
||||
|
||||
echo "-------".$surveyIndex."-----------";
|
||||
|
||||
// $data = $result['result'][''] ?? [];
|
||||
echo "Total responses: " . count($data['responses']) . "\n\n<br>";
|
||||
|
||||
if (isset($data['responses']) && is_array($data['responses'])) {
|
||||
foreach ($data['responses'] as $response) {
|
||||
echo "Response ID: " . $response['responseId'] . "\n";
|
||||
//Insert responseId into the database
|
||||
//
|
||||
$startDate = new DateTime($response['values']['startDate']);
|
||||
$startDateFormatted = $startDate->format('Y-m-d H:i:s');
|
||||
$endDate = new DateTime($response['values']['endDate']);
|
||||
$endDateFormatted = $endDate->format('Y-m-d H:i:s');
|
||||
$recordedDate = new DateTime($response['values']['recordedDate']);
|
||||
$recordedDateFormatted = $recordedDate->format('Y-m-d H:i:s');
|
||||
|
||||
$responseData = [
|
||||
'responseId' => $response['responseId'],
|
||||
'surveyId' => $surveyIndex,
|
||||
'startDate' => $startDateFormatted,
|
||||
'endDate' => $endDateFormatted,
|
||||
'status' => $response['values']['status'],
|
||||
'ipAddress' => $response['values']['ipAddress'],
|
||||
'progress' => $response['values']['progress'],
|
||||
'duration' => $response['values']['duration'],
|
||||
'finished' => $response['values']['finished'],
|
||||
'recordedDate' => $recordedDateFormatted,
|
||||
'locationLatitude' => $response['values']['locationLatitude'],
|
||||
'locationLongitude' => $response['values']['locationLongitude'],
|
||||
'distributionChannel' => $response['values']['distributionChannel'],
|
||||
'userLanguage' => $response['values']['userLanguage']
|
||||
];
|
||||
|
||||
|
||||
$responseIndex = upsertResponse($responseData);
|
||||
// Now read in the answers. These all start with QID????? etc
|
||||
$answers = $response['values'];
|
||||
|
||||
insertAnswers($pdo, $surveyIndex, $responseIndex, $answers);
|
||||
|
||||
// Prepare and execute the INSERT statement
|
||||
//
|
||||
//
|
||||
}
|
||||
|
||||
//print_r(json_decode($statusResponse, true));
|
||||
//exit(0);
|
||||
|
||||
$statusData = json_decode($statusResponse, true);
|
||||
$status = $statusData['result']['status'];
|
||||
|
||||
if ($status == 'failed') {
|
||||
die("Export failed!");
|
||||
}
|
||||
|
||||
echo "Waiting for export to complete... Status: $status\n";
|
||||
}
|
||||
|
||||
// Step 3: Download the Exported File
|
||||
$fileId = $statusData['result']['fileId'];
|
||||
|
||||
$fileCh = curl_init($fileUrl . $fileId . '/file');
|
||||
curl_setopt($fileCh, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($fileCh, CURLOPT_HTTPHEADER, $headers);
|
||||
$fileContent = curl_exec($fileCh);
|
||||
curl_close($fileCh);
|
||||
|
||||
if (!$fileContent) {
|
||||
die("Error downloading the file: " . curl_error($fileCh));
|
||||
}
|
||||
|
||||
// Save the file locally
|
||||
file_put_contents("survey_responses.zip", $fileContent);
|
||||
|
||||
echo "Survey data has been downloaded successfully!\n";
|
||||
|
||||
// Path to the ZIP file
|
||||
$zipFile = './survey_responses.zip'; // Specify the path to your ZIP file
|
||||
$extractTo = 'survey_responses.json'; // Specify the folder to extract the contents to
|
||||
|
||||
// Create a new ZipArchive object
|
||||
$zip = new ZipArchive();
|
||||
|
||||
// Open the ZIP file
|
||||
if ($zip->open($zipFile) === TRUE) {
|
||||
// Extract all the contents to the specified folder
|
||||
$zip->extractTo($extractTo);
|
||||
$zip->close(); // Close the ZIP file
|
||||
|
||||
echo "ZIP file extracted successfully!";
|
||||
} else {
|
||||
echo "Failed to open the ZIP file.";
|
||||
echo "No responses found.";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
?>
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user