0 votes
in SoSci Survey (English) by s057462 (330 points)

Hi Dominik
we would like to use responses retained in the first half of the survey (160 trials) to send the in a specific format to a server which makes some calculations and sends the result back to soscisurvey, and use that response to create subsequent questions (specify some amounts shown there).
In the past we have done a simpler version where we only sent s string of numbers to the server, but now we need to send all trial's responses and the definition of which type of trial they came from, so we would need to put that into a format that contains all that information and thought we'd use xml.
Is there a way to create a xml file from the obtained responses which can be sent to a server? The API section mentions JSON and csv only and I saw in another post that $curl = curl_init($url) is not usable, but maybe you have an idea how we could do this in another way?
Thanks you very much for your help.
All the best

1 Answer

0 votes
by SoSci Survey (312k points)

so we would need to put that into a format that contains all that information and thought we'd use xml.

JSON would probably be more convenient ... just a thought.

How did you do the sending so far?

curl_init($url) is not usable

No, but there will be some replacement in SoSci Survey soon. This is why I am asking for details - in order to make the function most useful for you :)

Is there a way to create a xml file from the obtained responses which can be sent to a server?

If you created an array with all question IDs that you'd like to send, that's white easy to accomplish by using a FOR loop and valueList(). But, as stated above, JSON might be much easier to encode AND to decode.

by s057462 (330 points)
Yes, I saw that you have JSON as an option.
So far we only needed to use a couple of aggregated numbers across trails, which we calculated in php and then sent off:

$crosspoints = "1000,{$meanAD1},{$meanAD2},{$meanAD3},{$meanAD4},{$meanAD5}"; // array for crossover points
$data = "{$num};{$times}{$crosspoints};{$x};24;24";

$response = sendPOST('http://jtc.virtualpsyclab.net/tomcat1/ddPRZ', array('data' => $data));


Yes, I was thinking of using a FOR loop, but we could not =create an xml file to send, right?
by SoSci Survey (312k points)
edited by SoSci Survey
>  but we could not =create an xml file to send, right?

Sure, you can. Sart with something like:

$xml = '<?xml version="1.0" encoding="UTF-8"?>';

Then continue with some root tag:

$xml.= '<myData>';

And then add XML with your data:

$xml = '<data attr="AB01">'.htmlspecialchars(value('AB01')).'</data>';

Nothing special ;)

If you create an XML like that, I will add a functio n to allow for sending JSON or XML contents. The sendPOST() method will use the GET format that is also used for web forms, i.e. param1=value1&param2=value2...

Of course, you can do something like:

sendPost($url, ['xml' => $xml]);
by SoSci Survey (312k points)
We have now added a function to send XML requests, using the appropriate MIME type: https://www.soscisurvey.de/help/doku.php/de:create:functions:sendjson

For beta testing, this function is available on www.soscisurvey.de, only, at the moment.
by s057462 (330 points)
Brilliant, that's great, thank you very much! I will work on the implementation and come back to you if I run into questions. Thanks!
by s057462 (330 points)
Hi Dominik
here is what I tried, however, I get an error message about invalid php code:
// create xml
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
//Then continue with some root tag:
$xml.= '<myData>';

//And then add data to XML:
//set the ID in $xmlPacket, called once
$xml = '<data attr="ID">'.htmlspecialchars(caseNumber()).'</data>';

// write the responses
for ($n = 0; $n < 80; $n++) {
    $xml = '<data attr=('SO01_'.sprintf('%02d', $n)))>'.htmlspecialchars(valueList('SO01_'.sprintf('%02d', $n)))).'</data>';  
};

for ($n = 0; $n < 80; $n++) {
    $xml = '<data attr=('SO02_'.sprintf('%02d', $n)))>'.htmlspecialchars(valueList('SO02_'.sprintf('%02d', $n)))).'</data>';  
};

// send xml
$xml = sendXML('http://dd.virtualpsyclab.net/ddgem/ddgem', [
  'q' => 'reflect',
  'd' => 'TempDiscountData'
]);
debug($xml);
by SoSci Survey (312k points)
>  I get an error message about invalid php code:

What does the rror say and what line does it refer to?

Please also note that sendXML() expects a string as second parameter and not an array: https://www.soscisurvey.de/help/doku.php/de:create:functions:sendjson

$answer = sendXML('http://dd.virtualpsyclab.net/ddgem/ddgem', $xml);
by s057462 (330 points)
The error message is:
"Warning: The string ?> is considered as extremely unsafe and is therefore not allowed in the questionnaire. If the string is a part of a text, you can save it as a questionnaire text and then integrate the questionnaire text in your questionnaire.
Warning: The PHP code on the page 7 contains functions or PHP elements that you are not permitted to use in this questionnaire.noenthält Funktionen oder PHP-Elemente, die nicht für die Verwendung im Fragebogen freigegeben wurden. If you think that the constructs in question are safe to use, please contact the administrator!"
by SoSci Survey (312k points)
Bitte ändern Sie diese Zeile

$xml = '<?xml version="1.0" encoding="UTF-8"?>';

am Ende wie folgt:

$xml = '<?xml version="1.0" encoding="UTF-8"?'.'>';
by s057462 (330 points)
Okay, yes that gets rid of the first error.
Now about building the xml:
$xml = '<data attr=('SO01_'.sprintf('%02d', $n))>'.htmlspecialchars(valueList('SO01_'.sprintf('%02d', $n)).'</data>';  

Does create an error:
Warning: Unexpected element: SO01_ - could it be that a semicolon (;) or quotation mark is missing?
$xml = '<data attr=('SO01_

So I need to somehow change how I refer to the variables in the loop, I guess? Could you please point me towards how to make that a for-loop-able section? Or would you suggest a different way of adding all SO01 items and SO02 items to the xml?
Thanks!
by SoSci Survey (312k points)
> So I need to somehow change how I refer to the variables in the loop

Please use the value() function and id() for the variable ID: value(id('SO01', $n))

And I also think that the first quotation mark should be a double one:

$xml = '<data attr="'.id('SO01', $n).'">...
by s057462 (330 points)
Thank you for your help with this.
I edited it to
for ($n = 0; $n < 2; $n++) {
    $xml = '<data attr="'.id('S101', $n).'">'.htmlspecialchars(value(id('S101', $n))).'</data>';  
};


And get this message:
The funktion id() expects a value of type integer > 0 as parameter no. 2. Yet, a value (0) of type integer was found.
by SoSci Survey (312k points)
It should be $n+1, as you start counting with 0, but the items start counting with 1.

id('S101', $n+1)
by s057462 (330 points)
Ah, of course! thanks!
it seems to send, we will work our way through the next step and might come back for the receiving side. Thank you!
by s057462 (330 points)
So now, receiving a response back from the server, that information comes back as an html, so me using it with "explode"

list($descriptives, $ssLPt0, $ssLPtd, $ssPRZt0, $ssPRZtd, $ssSPt0, $ssSPtd, $llLPt0, $llLPtd, $llPRZt0, $llPRZtd, $llSPt0, $llSPtd) = explode(";", $answer);

gives me the error:
"Questionnaire Error: explode() expects parameter 2 to be string, array given"

Is there a way to deal with an incoming html, by chance?
Thank you!
by SoSci Survey (312k points)
The response is XML, or at least parsed as XML (HTML may be XML as well). Therefore sendXML() will try and parse it, and return an array.

Please try this to see more details:

debug($answer)
by s057462 (330 points)
Thanks, the debug function shows that:
Debug information for $answer:
namehtml, attr[Array], value , children[Array]


Also: it seems like the information we are sending is a string (rather than an xml) as what gets there is:
<data attr="S102_03">1</data>

Here is my code as it stands:

// create xml
$xml = '<?xml version="1.0" encoding="UTF-8"?'.'>';

//Then continue with some root tag:
$xml= '<myData>';

//And then add XML with your data:
//set the ID in $xmlPacket, called once
$xml = '<data attr="ID">'.htmlspecialchars(caseNumber()).'</data>';

// write the responses
for ($n = 0; $n < 3; $n++) {
    $xml = '<data attr="'.id('S101', $n+1).'">'.htmlspecialchars(value(id('S101', $n+1))).'</data>';
};

for ($n = 0; $n < 3; $n++) {
    $xml = '<data attr="'.id('S102', $n+1).'">'.htmlspecialchars(value(id('S102', $n+1))).'</data>';  
};


// send xml
$answer = sendXML('http://dd.virtualpsyclab.net/ddgem/ddgem', $xml);

debug($answer);


Do you know what need to be changed to get sent as the actual xml?
Thank you!
by SoSci Survey (312k points)
> Do you know what need to be changed to get sent as the actual xml?

The actual XML is sent, but it seems that the counterpart already strips away the root tag <myData> surrounding the data.

> Thanks, the debug function shows that

Looks reasonable. The <html> tag is the root tag of an HTML document. That means that the array $answer['children'] should contain two elements. One for the <head> and one for the <body> tag. And the <body> then probably contains the contents you are looking for.
by s057462 (330 points)
Okay, I edited what is sent so it actually adds all columns using this:
// create xml
$xml = '<?xml version="1.0" encoding="UTF-8"?'.'>';

//Then continue with root tag:
$xml+= '<myData>';

//And then add data to XML:
//set the ID in $xmlPacket, called once
$xml += '<data attr="ID">'.htmlspecialchars(caseNumber()).'</data>';

// write the responses
for ($n = 0; $n < 3; $n++) {
    $xml += '<data attr="'.id('S101', $n+1).'">'.htmlspecialchars(value(id('S101', $n+1))).'</data>';
};

for ($n = 0; $n < 3; $n++) {
    $xml += '<data attr="'.id('S102', $n+1).'">'.htmlspecialchars(value(id('S102', $n+1))).'</data>';  
};

//closing tag
$xml+= '</myData>';
 
 
// send xml
$answer = sendXML('http://dd.virtualpsyclab.net/ddgem/ddgem', $xml);

debug($answer);


But I get the error:
The contents specified in sendXML() is no valid XML.

and
Debug information for $answer:
$answer = false (boolean)
by SoSci Survey (312k points)
Please note that PHP does not use a plus (+) to concatenate strings, but the dot (.), i.e.

$xml.= '<myData>';

Willkommen im Online-Support von SoSci Survey.

Hier bekommen Sie schnelle und fundierte Antworten von anderen Projektleitern und direkt von SoSci Survey.

→ Eine Frage stellen


Welcome to the SoSci Survey online support.

Simply ask a question to quickly get answers from other professionals, and directly from SoSci Survey.

→ Ask a Question

...