EoQ_Supporting_Files/getQuestionsData.php

73 lines
2.3 KiB
PHP

<?php
// Load config
$config = require 'config.php';
$apiToken = $config['api_token'];
$dataCenter = $config['data_centre'];
// Get the survey ID from POST
$surveyId = $_POST['survey_id'] ?? null;
if (!$surveyId) {
//$surveyId="SV_cAstEvm4ZrPaqGi";
$surveyId="SV_cwKjMqAqGxImjMG";
#die("No survey ID provided.");
}
// Build URL
$baseUrl = "https://$dataCenter.qualtrics.com/API/v3";
$questionsUrl = "$baseUrl/survey-definitions/$surveyId/questions";
// Set headers
$headers = [
"X-API-TOKEN: $apiToken",
"Content-Type: application/json"
];
// Make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $questionsUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die("Request error: " . curl_error($ch));
}
curl_close($ch);
// Output the raw JSON
//header('Content-Type: application/json');
//echo $response;
//var_dump($response);
$data = json_decode($response, true);
if (isset($data['result']['elements']) && is_array($data['result']['elements'])) {
foreach ($data['result']['elements'] as $element) {
if (isset($element['QuestionID'])) {
echo "QuestionID: " . $element['QuestionID'] . "<br>". PHP_EOL;
if (isset($element['QuestionID']) && $element['QuestionID'] === "QID70") {
//echo json_encode($element, JSON_PRETTY_PRINT) . PHP_EOL;
}
if (isset($element['QuestionID']) && $element['QuestionID'] === "QID68") {
echo json_encode($element['QuestionDescription'], JSON_PRETTY_PRINT) . PHP_EOL;
echo json_encode($element['Choices'], JSON_PRETTY_PRINT) . PHP_EOL;
}
if (isset($element['QuestionID']) && $element['QuestionID'] === "QID69") {
echo json_encode($element['QuestionDescription'], JSON_PRETTY_PRINT) . PHP_EOL;
echo json_encode($element['Choices'], JSON_PRETTY_PRINT) . PHP_EOL;
}
if (isset($element['QuestionID']) && $element['QuestionID'] === "QID70") {
echo json_encode($element['QuestionDescription'], JSON_PRETTY_PRINT) . PHP_EOL;
echo json_encode($element['Choices'], JSON_PRETTY_PRINT) . PHP_EOL;
}
}
}
} else {
echo "No elements found.";
}
?>