"json", "compress" => false ]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $exportUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 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); $result = json_decode($response, true); echo "
".$surveyId." ".$exportUrl.""; if (!isset($result['result']['progressId'])) { echo "
Export API Response:\n";
print_r($response);
echo "";
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");
}
$fileId = $result['result']['fileId'];
// 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);
if (curl_errno($ch)) {
die("Error downloading the file: " . curl_error($ch));
}
curl_close($ch);
// Decode the response
$data = json_decode($response, true);
//For testing only
echo $response;
//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